from io import StringIO
import csv
f = StringIO()
writer = csv.writer(f, quotechar='"', quoting=csv.QUOTE_ALL, lineterminator="\n")
writer.writerow(['foo,', 'bar'])
writer.writerow(['"Ai', 'Ueo\''])
result = f.getvalue()
print(type(result))
print(result)
According to Document, the following parameters can be specified. csv.writer(csvfile[, dialect='excel'][, fmtparam]) It seems that the csvfile object can be any object that has a write method. Therefore, sys.stdout etc. can also be specified.
I did a little research from the request that I want to be able to copy and paste on the WEB instead of exporting as CSV and saving it in a file. I found how to use StringIO in the csv module test (Python-3.5.2 / Lib / test / test_csv.py).
Recommended Posts