有没有办法迭代不同的行?我试过 row+=1 但它没有用

Is there any way for iterating over different rows?I have tried row+=1 but it did not work

我已经使用 CSV.DictReader 读取了一个 CSV 文件;我想遍历行并将每行的值与一些变量进行比较,我想重复这个过程直到行的值与变量的值匹配。我试过 row+=1 但没有用,有人能建议我另一种方法吗?

with open(argv[1],"r") as file:
  reader = csv.DictReader(file)
    for row in reader:
      for line in reader:
        A=int((row["AGATC"]))
        B=int((row["AATG"]))
        C=int((row["TATC"]))
      if(A==AGATCtotal and B==AATGtotal and C==TATCtotal):
        print(row["name"])[][1] 

我想下面的就可以了。

with open(argv[1],"r") as file:
  reader = csv.DictReader(file)
  for row in reader:
    A=int((row["AGATC"]))
    B=int((row["AATG"]))
    C=int((row["TATC"]))
    if(A==AGATCtotal and B==AATGtotal and C==TATCtotal):
      print(row["name"]) 
  • for line in reader: 是多余的,因为您已经迭代了上一行的行。
  • if语句缩进错误;它应该在 for 循环内。请注意,Python 通过缩进识别代码块。