【发布时间】:2021-04-16 01:34:02
【问题描述】:
我正在尝试将列表写入 csv,但是当字符串中有双引号时,它会将文本拆分到另一个单元格。有没有办法在不删除双引号的情况下将其保留在一个单元格中?
我的清单
alst = [['John', 'Smith', 'Google, and Samsung'],['John', 'Smith', '"Google", and "Samsung"'],['John', 'Smith', 'Google ", and Samsung']]
output = open('output.csv', 'w')
output.write('first, last, desc\n')
for item in alst:
output.write('"{0}","{1}","{2}"\n'.format(item[0], item[1], item[2]))
output.close()
在excel中输出文件
first | last | desc | (Blank)
John | Smith | Google, and Samsung
John | Smith | Google" | and "Samsung""
John | Smith | Google | and Samsung"
我想要得到什么
first | last | desc
John | Smith | Google, and Samsung
John | Smith | "Google", and "Samsung"
John | Smith | Google ", and Samsung
字符串中只有一个双引号,它以逗号分隔。我该如何防止这种情况?我需要处理列表吗?我仍然想在列表中保留双引号。
【问题讨论】: