Reputation: 211
I'm trying to write into a string to csv file using csv modules. The string is getting printed in the excel sheet as single characters in different rows and not in one field.
with open('test.csv', 'w',newline='') as f:
writer=csv.writer(f)
writer.writerow("hello")
writer.writerow("bye")
The output is like
h e l l o
b y e
What I simply want is
hello
bye
Also how do I write in a different column? there is no writecolumn() for it like writerow()
Upvotes: 1
Views: 2381
Reputation: 95
Adding to the previous answer,
If you want to do the same with csv.writerows() function:
rows = ["apple", "banana", "mango", "orange"]
with open('test.csv', 'w',newline='') as f:
writer=csv.writer(f)
writer.writerows([[row] for row in rows])
That should print all the rows, in 1 column per row.
Upvotes: 2
Reputation: 254
In order to output in two rows:
writer.writerow(["hello"])
writer.writerow(["bye"])
In order to output in two cols:
writer.writerow(["hello","bye"])
Upvotes: 1