CSV 编写器双引号

xth*_*ing 1 python csv quoting delimiter

我有一个Python字符串:hello, my name is "Joe". 当我尝试使用该csv模块进行编写时,我得到了"hello, my name is ""Joe""". 我希望看到的是"hello, my name is "Joe""

当存在双引号时,是否有办法让 CSV 编写器不添加双引号?

代码:

s = 'hello, my name is "Joe"'
with open(filename, 'w', newline='', encoding='utf-8') as f_out:
    writer = csv.writer(f_out)
    writer.writerow([s])
Run Code Online (Sandbox Code Playgroud)

And*_*ely 6

quotechar您可以在创建csv.writerdoc )时使用参数:

import csv

s = 'hello, my name is "Joe"'
with open('out.csv', 'w', newline='', encoding='utf-8') as f_out:
    writer = csv.writer(f_out, quotechar="'")
    writer.writerow([s])
Run Code Online (Sandbox Code Playgroud)

文件的输出是:

'hello, my name is "Joe"'
Run Code Online (Sandbox Code Playgroud)