任何人都可以帮助我,我是python的新手,我在分配变量和跳过某些行时遇到了一些问题
我的代码如下所示:
import csv
with open('sample.csv', "r") as csvfile:
# Set up CSV reader and process the header
reader = csv.reader(csvfile, delimiter=' ')
skip_lines = csvfile.readlines()[4:] #skip the first four lines
capacity = []
voltage = []
temperature = []
impedance = []
# Loop through the lines in the file and get each coordinate
for row in reader:
capacity.append(row[0])
voltage.append(row[1])
temperature.append(row[2])
impedance.append(row[3])
print(capacity, voltage, temperature, impedance)
答案 0 :(得分:0)
你的问题在这里:
skip_lines = csvfile.readlines()[4:]
将整个文件读入内存,并将前4行中的所有内容放入skip_lines
。当你到达时:
for row in reader:
文件处理程序已用尽。
要跳过文件处理程序中的单行,请使用:
next(csvfile)
因为你想跳过前四个:
for _ in range(4):
next(csvfile)