Python 如何告诉for循环从一开始就读取文件?
读取以下行中的CSV文件时:Python 如何告诉for循环从一开始就读取文件?,python,loops,dictionary,csvreader,Python,Loops,Dictionary,Csvreader,读取以下行中的CSV文件时: csv_file = open(sys.argv[1], "r") # reading the csv file into memory csv_reader = csv.DictReader(csv_file) 当我尝试在使用for循环时复制csv_读取器时,它工作了,但是对该复制版本的任何更改都会影响csv_读取器变量,另外 for line in csv_reader: copy = line break for line in csv
csv_file = open(sys.argv[1], "r") # reading the csv file into memory
csv_reader = csv.DictReader(csv_file)
当我尝试在使用for循环时复制csv_读取器时,它工作了,但是对该复制版本的任何更改都会影响csv_读取器变量,另外
for line in csv_reader:
copy = line
break
for line in csv_reader:
print(line)
第二个for循环将打印除csv\u文件中第一行以外的所有内容,为什么?您可以在for循环之间使用
csv\u文件。seek(0)
csv_file = open(sys.argv[1], "r")
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
copy = line
break
csv_file.seek(0)
for line in csv_reader:
print(line)
# Outputs full csv file
这就是我要找的,谢谢!!