csv.writer.writerow 字段中的逗号书写不正确

csv.writer.writerow with a comma in a field not writing correctly

我正在从网站提取数据并将其保存到 Python 中的 CSV 文件中。大部分时间都有效,除非其中一行中的特定字段偶尔有几千个,并且供应商在该字段中包含一个逗号。无论我如何使用 writer.writerow() 我似乎都无法排除这个逗号被解释为定界符,即使供应商在出现逗号时用双引号括起特定字段。

# Obtain the data from the website
downloadresponse = s.post(downloadurl, data=downloaddata,headers=postheaders, cookies=loginresponse.cookies)
print("Download response status =", downloadresponse.status_code)
print("Downloading " +filename+" now.")

with open(filename, 'w') as f:
    writer = csv.writer(f, delimiter =',',escapechar='\' ,quotechar ='"', quoting=csv.QUOTE_MINIMAL, doublequote=True)
    for line in downloadresponse.iter_lines():
        print("Raw Line =", line)
        print("UTF Line =", line.decode('utf-8'))
        print("UTF & Split Line =", line.decode('utf-8').split(','))
        writer.writerow(line.decode('utf-8').split(','))

为 CSV 中有问题的行生成此输出。

Download response status = 200 
Download response reason = OK 
Downloading /Users/someone/Documents/@Investing/UOA-Workspace/data/uoa-eod-02-04-2021.csv now.

DEBUG-Raw Line = b'AMC,7.31,Call,2,02/05/21,1,5.25,5.33,5.4,5.3,739,283,2.61,"1,246.65%","15:10 ET"' \
DEBUG-UTF Line = AMC,7.31,Call,2,02/05/21,1,5.25,5.33,5.4,5.3,739,283,2.61,"1,246.65%","15:10 ET" \
DEBUG-UTF & Split Line = ['AMC', '7.31', 'Call', '2', '02/05/21', '1', '5.25', '5.33', '5.4', '5.3', '739', '283', '2.61', '"1', '246.65%"', '"15:10 ET"']

因此,原始 LINE 中的字段“1,246.65%”被逗号拆分为两个字段“1”、“246.65%”,即使它包含在双引号中。谁能看出我做错了什么?

如评论中所说,可以直接写,不用csv模块。行看起来正确引用。 像

with open(filename, 'w') as f:
    f.write(downloadresponse.text)

这可能需要一些调整。例如如果是流式响应,您可能需要逐行编写。

对于其他人,@buran 提供了答案。我假设我需要 csv.writer 但显然因为 API 响应已经格式化好我只需要将 response.text 写入一个文件并且它被完美格式化。更新代码:

# Obtain the data from the website
downloadresponse = s.post(downloadurl, data=downloaddata,headers=postheaders, cookies=loginresponse.cookies)
print("Download response status =", downloadresponse.status_code)
print("Downloading " +filename+" now.")

with open(filename, 'w') as f:
    f.write(downloadresponse.text)
    f.close()