Python Forum

Full Version: Help|CSV Writing on List with Inner Commas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Let's say I have:
l = ['Washington, 98373, USA', 'Arlington, WA, 98223, USA', 'Soap Lake, Washington, 98851, USA']
What I'm doing is just typical writing
with open('results.csv', 'w', newline='') as f:
    f.write(str(l))
NOTE: I probably cannot write with a list, giving me error(s):
TypeError: write() argument must be str, not list
So my csv would like like:
[Image: 1scoYhF.png]

What I want to write is:
[Image: 2B4JODZ.png]

Hope you can help me out guys and Thanks in advance for your suggestions!

NOTE: It seems the
Update: I see that writerow() than write() achieve the said goal. But now the problem is it writes in column. Yes, I was also wrong with the second image, I want the end result to be in rows. I achieve rows using write() but I cannot achieve combining each on the list with it.

I hope I make sense going in circles with my explanation. Big Grin
You can try the following:

f.write('{}\n'.format(';'.join(l)))
Another option is to use csv-module.