我试图从这个csv文件中只写出两个特定的行。这是我的代码
with open('StudentsMajorsList.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
with open('ComputerScienceStudents.csv', 'w') as new_file:
fieldnames = ['StudentID','Major','FirstName','LastName','DisciplinaryAction']
csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter=',')
csv_writer.writeheader()
for row in csv_reader:
csv_writer.writerow(row)
在这个区域
for row in csv_reader:
csv_writer.writerow(row)
我只想拉两排,而不是全部。请帮忙!
我想你正在寻找这样的东西:
for row in csv_reader:
if some_condition:
csv_writer.writerow(row)
你还没有告诉我们some_condition
是什么,所以我们只能猜测。