user1474018
user1474018

Reputation: 55

Python writerows trouble

I begin in Python language and face a problem with my script. On stackoverflow, i fould only a post where somebody asks how to write in columns, but form me it is the problem :)

I have two lists. The first one contains some other lists which content must be written in a csv file, the second one the name of the files to be created.

contentlist = [element1, element2 ... ]
namelist = ['element1', 'element2' ... ]

Each item of the list contentlist follows this model :

'url 1 | text1'

For output i would like something like this, url and text being divided by the | character :

Column1          Column2

url1             text1

url2             text2

My script is partially successful.

    for name, content in zip(namelist, contentlist):
        with open(name, 'w') as f:
        w = csv.writer(f, dialect = 'excel', delimiter=',')
        w.writerows([content]) 

But I get this output :

column1         column2

url1 | text1    url2 | text2

If i delete [ ] at the last line of my script, i manage ton get the output i'm wanting, but obviously there is a comma between each character.

Could someone help me ? Thanks in advance.

Upvotes: 1

Views: 1784

Answers (1)

Amber
Amber

Reputation: 527123

w.writerows(c.split(' | ') for c in content)

Upvotes: 3

Related Questions