Python Forum
Help|CSV Writing on List with Inner Commas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help|CSV Writing on List with Inner Commas
#1
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
Reply
#2
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
Reply
#3
You can try the following:

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with commas in print functions kronhamilton 11 3,348 Feb-10-2022, 02:02 PM
Last Post: mishraakash
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,551 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,077 Jul-02-2019, 02:29 PM
Last Post: metulburr
  Automate the boring stuff, inserting commas in list DJ_Qu 3 4,655 Apr-21-2019, 03:52 PM
Last Post: perfringo
  accessing array without commas rjnabil1994 1 2,468 Feb-10-2019, 06:29 PM
Last Post: Larz60+
  Help writing code to create a ranked list swilson421 2 2,415 Jul-16-2018, 04:51 PM
Last Post: swilson421
  Printing array without commas pawlaczkaj 1 9,387 Apr-08-2018, 07:05 PM
Last Post: Larz60+
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,146 Aug-30-2017, 12:14 PM
Last Post: metulburr
  Writing List to CSV tkj80 6 5,618 Apr-11-2017, 11:14 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020