user3351370 user3351370 - 3 months ago 10
Python Question

Write list and value in same row csv

I have several values and severals list with differents length.
I'd like to write them in the same CSV row.

i.e.
I have these values

a=1234

b=["John","Bryan","Johnny"]

c="Value"

d=["Comedy","Action"]


As output I'd like a csv file like this :

[1234;"John";"Bryan";"Johnny";Value;Comedy;Action]


So far I coded this

resultFile = open("DNA_Movie.csv",'wb')
wr = csv.writer(resultFile, dialect='excel')
wr.writerows([imdb_id, genre, actors])


But the result is like this:

1234
"John","Bryan","Johnny"
"Value"
"Comedy","Action"

Answer

Use writerow and combine each element together into a single list:

wr.writerow([a] + b + [c] + d)

Some of the elements need to be wrapped in brackets to turn them into a list with a single element.