'Problem writing to csv file, string is over multiple columns

I am trying to write some data to a csv file. however one of the words is being written over multiple columns when it's meant to be in just one. And the rest of the data isn't showing up.

Here is the code I have so far:


header = ['Type','cap pos', 'cap neg', 'growth pos', 'growth neg']
data = ["My code", counter_cp, counter_cn, counter_gp, counter_gn]
            
with open('Comparison1.csv', 'w', encoding='UTF8', newline='') as f:
     writer = csv.writer(f)
     writer.writerow(header)
     writer.writerows(data)

counter_cp etc are int types, from a for loop I did before which counted the number of a certain thing. When I do print(counter_cp, type(counter_cp) I get: Counter : 430 type : <class 'int'>.

The output is coming out like:

Type cap pos cap neg growth pos growth neg
M y c o d e

I'm not really sure where I'm going wrong? It's meant to come out like:

Type cap pos cap neg growth pos growth neg
My code 430 260 452 297


Solution 1:[1]

The problem is you used writer.writerows() instead of writer.writerow().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hai Vu