我在大型(7000行)csv文件上尝试了这个建议的解决方案): Read File From Line 2 or Skip Header Row`
以下代码有效,但只有在我手动删除标题时才会有效:
RatedTracksFile = open('pyall_rated.csv')
RatedTracksReader = csv.reader(RatedTracksFile)
data_list = list(csv.reader(RatedTracksFile))
eligible1 = open('py_all1.csv', 'a') # open py_all1.csv in append mode
for i in range(len(data_list)):
trackrating = int(data_list[i][12])
...
我尝试了添加此建议代码的不同方法:
with open(fname) as f:
next(f)
for line in f:
我是python的新手,无法弄清楚如何添加它。我一直得到相同的值类型错误,因为它仍然读取第1行,第12列(来自标题的字符串)而不是找到的整数第2行,第12栏:
ValueError: invalid literal for int() with base 10: 'GroupDesc'
已解决:基于Sphinx的评论(谢谢!)。
答案 0 :(得分:6)
import csv
with open("1800.csv", "rb") as files:
reader = csv.reader(files)
next(reader, None)
for data in reader:
print data
这对我有用,试试这个。
答案 1 :(得分:4)
如果您的标题包含您的列名,最好的解决方案是使用csv.DictReader
>>> import csv
>>> with open('names.csv') as csvfile:
... reader = csv.DictReader(csvfile)
... for row in reader:
... print(row['first_name'], row['last_name'])
...
Baked Beans
Lovely Spam
Wonderful Spam