有没有办法使用Python';s csv.reader()在没有引号字段的csv文件上?

有没有办法使用Python';s csv.reader()在没有引号字段的csv文件上?,python,csv,Python,Csv,我试图从一个csv文件中读取,该文件包含对问题的回答以及属于这些回答的分数。看起来像 id,answer,cat1,cat2,cat3,cat4 10000001,"This is my response to the question. I think it's a good response!",100,100,50,50 我只想把这些值读入一个列表 然而,在这个应用程序中,一个文件被上传到一个网页,作为workzeug.FileStorage对象访问,然后作为byte

我试图从一个csv文件中读取,该文件包含对问题的回答以及属于这些回答的分数。看起来像

id,answer,cat1,cat2,cat3,cat4
10000001,"This is my response to the question. I think it's a good response!",100,100,50,50
我只想把这些值读入一个列表

然而,在这个应用程序中,一个文件被上传到一个网页,作为
workzeug.FileStorage
对象访问,然后作为
bytes
对象传递到后端。在我的代码中,我尝试将其转换为字符串,然后使用
csv.reader()
将其转换为列表

以下是相关代码:

def foo(_文件):
file=\u file.decode()
打印(文件)
文件=列表(csv.reader(文件))
打印(文件)
问题是:
csv.reader()
似乎在非逗号的内容上出现了分歧。事实上,它将所有未包含在引号中的内容拆分为单个字符,包括逗号。下面是第二个
print()
输出的内容:

[['i'], ['d'], ['', ''], ['a'], ['', ''], ['c'], ['a'], ['t'], ['1'], ['', ''], ['c'], ['a'], ['t'], ['2'], ['', ''], ['c'], ['a'], ['t'], ['3'], ['', ''], ['c'], ['a'], ['t'], ['4'], [], ['1'], ['0'], ['0'], ['0'], ['0'], ['0'], ['0'], ['1'], ['', ''], ["This is my response to the question. I think it's a good response!"], ['', ''], ['1'], ['0'], ['0'], ['', ''], ['1'], ['0'], ['0'], ['', ''], ['5'], ['0'], ['', ''], ['5'], ['0']]
默认情况下,
csv.reader()
通常不需要引号。。。这是怎么回事?谢谢

我正在使用Python3.7.9。reader()要求它的参数是一个返回行的迭代器。您将整个文件内容作为单个字符串提供给它,因此它将每个字符视为一行

您需要在换行符处拆分它以获得一系列行

def foo(_file):
    file = _file.decode()
    print(file)
    file = list(csv.reader(file.split('\n')))
    print(file)

csv.reader需要文件对象而不是文件名,例如

# file is the csv filename, e.g. /tmp/ex.csv
for t in csv.reader(open(file)):
  print(t)

# Outputs:
['id', 'answer', 'cat1', 'cat2', 'cat3', 'cat4']
['10000001', "This is my response to the question. I think it's a good response!", '100', '100', '50', '50']

第一个
print()
输出什么?尝试用
\u file.read().decode()
代替
\u file.decode()