python - 如何修复这个 qouting 在 python 中读取 csv 文件时必须是整数
问题描述
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
参数。
推荐阅读
- javascript - React 中哪种方法更好?
- javascript - Rails 使用带有acts_as_votable 的Ajax 支持/反对
- laravel - 在 laravel 中实现存储库模式的官方方法
- ckan - 在 CKAN 中创建标准用户作为系统管理员
- java - 如何为 AndroidX 测试配置 Android 项目
- varnish-vcl - 如何在清漆中使用正则表达式从长度未定义的字符串中提取 n 个字符?
- php - 在 Laravel 项目中创建密码哈希
- ruby - 工厂女孩和 mongoid 的问题
- java - 我想显示来自drawable的图像并将其与来自json-api的数据结合到recylerview
- java - jar 文件,pom 文件未在 maven 项目中下载