def read_csv_fieldnames(filename, separator, quote):
"""
Inputs:
filename - name of CSV file
separator - character that separates fields
quote - character used to optionally quote fields
Ouput:
A list of strings corresponding to the field names in
the given CSV file.
"""
with open(filename, newline="") as csv_file:
row_fieldnames = []
reader = csv.reader(csv_file, delimiter=separator, quoting=quote)
for row in reader:
row_fieldnames.append(row)
return row_fieldnames[0]
test_file1 = read_csv_fieldnames("table1.csv", ',', '"')
这是我的 csv 文件 Table1.csv = Field1,Field2,Field3,Field4 中的数据
预期输出 = ['Field1', 'Field2', 'Field3', 'Field4']
运行代码后=TypeError,引用必须是整数
quote - 用于可选地引用字段的字符
是的,但您将其传递给quoting
此处的参数:
reader = csv.reader(csv_file, delimiter=separator, quoting=quote)
然而quoting
期望一个整数。要指定引号字符,请使用quotechar
参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句