Python 由于双引号,正在写入csv拆分文本

Python 由于双引号,正在写入csv拆分文本,python,csv,Python,Csv,我试图将列表写入csv,但当字符串中有双引号时,它会将文本拆分到另一个单元格。有没有办法在不删除双引号的情况下将其保存在一个单元格中 我的名单 alst = [['John', 'Smith', 'Google, and Samsung'],['John', 'Smith', '"Google", and "Samsung"'],['John', 'Smith', 'Google ", and Samsung']] output = open(

我试图将列表写入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              
字符串中只有一个双引号,它以逗号分隔。我如何防止这种情况?我需要处理列表吗?我仍然希望在列表中保留双引号。

使用
csv.writer()
将列表传递给编写器即可

with open('output.csv', 'w', newline='') as output:
    writer = csv.writer(output)
    writer.writerow(['First', 'Last', 'Desc'])
    writer.writerows(alst)

我有没有办法不用csv软件包来解决这个问题?很可能是的。然而,
csv
库是内置的,专门为此而构建的。您不想使用它的原因是什么?我正在尝试执行一个没有导入功能的项目,但由于某些原因,我无法正确输出此部分。@Hal除非对不导入的项目有限制,否则请诚实地使用
csv
。您在这里遇到了文本条目中逗号的问题,因此需要找到一种方法来忽略该逗号或定义不同的分隔符,然后使CSV文件接受该分隔符。