csv.writer requires me to feed in a real csv file. is there a way I can feed it a string or a list to write to instead of an actual file? I want to return the csv for another function without writing a file.
hold = [some_list]
blank = []
w = csv.writer(blank, delimiter=',')
w.writerows(hold)
return w
Traceback (most recent call last):
w = csv.writer(blank, delimiter=',')
TypeError: argument 1 must have a "write" method
A StringIO object is suitable for this:
>>> f = StringIO()
>>> w = csv.writer(f)
>>> w.writerow(['hello', 'world'])
13
>>> f.getvalue()
'hello,world\r\n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With