使用以下数据,使用代码段,我收到以下错误。你能帮我解决这个问题吗?我是python的初学者。 数据:
"Id","Title","Body","Tags"
"Id1","Tit,le1","Body1","Ta,gs1"
"Id","Title","Body","Ta,2gs"
代码:
#!/usr/bin/python
import csv,sys
if len(sys.argv) <> 3:
print >>sys.stderr, 'Wrong number of arguments. This tool will print first n records from a comma separated CSV file.'
print >>sys.stderr, 'Usage:'
print >>sys.stderr, ' python', sys.argv[0], '<file> <number-of-lines>'
sys.exit(1)
fileName = sys.argv[1]
n = int(sys.argv[2])
i = 0
out = csv.writer(sys.stdout, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
ret = []
def read_csv(file_path, has_header = True):
with open(file_path) as f:
if has_header: f.readline()
data = []
for line in f:
line = line.strip().split("\",\"")
data.append([x for x in line])
return data
ret = read_csv(fileName)
target = []
train = []
target = [x[2] for x in ret]
train = [x[1] for x in ret]
错误:
target = [x[2] for x in ret]
IndexError: list index out of range
答案 0 :(得分:3)
您正在混合file.readline()
并将文件对象用作可迭代的。不要那样做。请改用next()
。
您还应该使用csv.reader()
模块来读取数据,不要重新发明这个轮子。 csv
模块可以处理引用的CSV值,并且在任何情况下都可以更好地嵌入值中的分隔符:
import csv
def read_csv(file_path, has_header=True):
with open(file_path, 'rb') as f:
reader = csv.reader(f)
if has_header: next(reader, None)
return list(reader)
最后但并非最不重要的是,您可以使用zip()
来转置行和列:
ret = read_csv(fileName)
target, train = zip(*ret)[1:3] # just the 2nd and 3rd columns
此处zip()
将停在不足够列的第一行,至少可以避免您看到的异常。
如果某些行中缺少列,请改用itertools.izip_longest()
(Python 3中为itertools.zip_longest()
):
from itertools import izip_longest
ret = read_csv(fileName)
target, train = izip_longest(*ret)[1:3] # just the 2nd and 3rd columns
默认设置是用None
替换丢失的列;如果您需要使用其他值,请将fillvalue
参数传递给izip_longest()
:
target, train = izip_longest(*ret, fillvalue=0)[1:3] # just the 2nd and 3rd columns