Disable csv
quoting and add the quotes yourself:
def quote(col):
if col is None:
return ''
# uses double-quoting style to escape existing quotes
return '"{}"'.format(str(col).replace('"', '""'))
writer = csv.writer(fileobj, quoting=csv.QUOTE_NONE, escapechar='', quotechar='')
for row in rows:
writer.writerow(map(quote, row))
By setting both escapechar
and quotechar
to empty strings you avoid the module quoting your already-quoted values.
The above works as long as you don't use the delimiter in the csv values.
Note that by this time it would just be easier to write comma-delimited lines yourself:
with open(filename, 'w'), fd:
for row in rows:
fd.write(','.join(map(quote, row)) + '
')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…