【发布时间】:2017-11-15 15:35:25
【问题描述】:
好的,我放弃了尝试让它读取包含关键字的行之后的行。我的两周就够了!!我试图以另一种方式解决这个问题。
每个条目包含五行。我想要的大部分文本都在第一行,我可以访问。以前我试图访问包含关键字“Active”的行之后的行上的行[63]。这个我不知道该怎么做。
所有五个条目在第 [4] 行中都包含相同的参考编号。我可以将第一行中的 row[4] 分配给“pid”。我希望那时能够使用“pid”来检查第二行。即,如果一行包含相同的“pid”并且在 row[63] 中包含关键字“Reported by”,则执行某些操作。是否可以在同一代码中使用两个 if 语句来执行此操作?代码运行但返回一个空白文本文件。 Iv 在第一个 if 语句内部和外部都尝试了第二个 if 语句。编辑;我可以让这些语句写入两个不同的文件。我只需要他们写入同一个文件:}
导入 csv
with open("test.csv", "r") as f, open("cops.txt", "w") as output:
r = csv.reader(f)
for row in r:
if "Active" in row and row[0] and row[32]:
pid = row[4]
num = row[13] #this part is working
#im trying to get this to check the lines after the line with "Active" in it
if pid == row[4] and "Reported by" in row and row[63]: #is it possible to use variable pid from the 1st if statement in this if statement?
rpt_by = row[63]
name = row[64]
output.write(f"PID: {pid} - " + "{} - {}".format(row[6], row[1])
+ " {}\n \n \n".format(row[19]) + "{} \n \n".format(row[32]) + f"Investigating Member: {num}" +"\n\n\n\n\n") #this part is working
+ (f"{rpt_by} + {name}") #I want to append this to my current code
【问题讨论】:
-
感谢您的回复。我觉得很难跟上。我会一直坚持到它开始有意义为止。
标签: python csv if-statement row lines