当我尝试跑步时
import csv
import sys
import operator
fieldnames = ["A","B","C","D","E"]
surveyfile = open("source.csv", "r")
left_file = open("left.csv",'wb')
right_file = open("right.csv",'wb')
left_reader = csv.DictReader(surveyfile, fieldnames=fieldnames, delimiter=",")
left_writer = csv.DictWriter(left_file, fieldnames, delimiter=",")
sortedlefts = sorted(left_reader,key=lambda x:float(x["B"]))
right_reader = csv.DictReader(surveyfile, fieldnames=fieldnames, delimiter=",")
right_writer = csv.DictWriter(right_file, fieldnames, delimiter=",")
sortedrights = sorted(right_reader,key=lambda x:float(x["B"]), reverse=True)
for row in sortedlefts:
if row["E"] == "l":
left_writer.writerow(row)
for row in sortedrights:
if row["E"] == "r":
right_writer.writerow(row)
“right.csv”文件中没有任何反应。但是,如果我将所有与制作 right.csv 文件有关的内容放入另一个程序中,它就可以正常工作。我需要结束那个for循环吗?两者使用相同的阅读器是否有问题?