Python list string to csv

I'm trying to create a csv file that contains the contents of a list of strings in Python, using the script below. However when I check my output file, it turns out that every character is delimited by a comma. How can I instruct csv.writer to delimit every individual string within the list rather than every character?

import csv

RESULTS = ['apple','cherry','orange','pineapple','strawberry']

result_file = open("output.csv",'wb')
wr = csv.writer(result_file, dialect='excel')
for item in RESULTS:
    wr.writerow(item)

I checked PEP 305 and couldn't find anything specific.

asked Aug 2, 2011 at 18:08

2

The csv.writer writerow method takes an iterable as an argument. Your result set has to be a list (rows) of lists (columns).

csvwriter.writerow(row)

Write the row parameter to the writer’s file object, formatted according to the current dialect.

Do either:

import csv
RESULTS = [
    ['apple','cherry','orange','pineapple','strawberry']
]
with open('output.csv','w') as result_file:
    wr = csv.writer(result_file, dialect='excel')
    wr.writerows(RESULTS)

or:

import csv
RESULT = ['apple','cherry','orange','pineapple','strawberry']
with open('output.csv','w') as result_file:
    wr = csv.writer(result_file, dialect='excel')
    wr.writerow(RESULT)

answered Aug 2, 2011 at 18:11

5

Very simple to fix, you just need to turn the parameter to writerow into a list.

for item in RESULTS:
    wr.writerow([item])

answered Aug 2, 2011 at 18:15

Mark RansomMark Ransom

290k40 gold badges384 silver badges607 bronze badges

I know I'm a little late, but something I found that works (and doesn't require using csv) is to write a for loop that writes to your file for every element in your list.

# Define Data
RESULTS = ['apple','cherry','orange','pineapple','strawberry']

# Open File
resultFyle = open("output.csv",'w')

# Write data to file
for r in RESULTS:
    resultFyle.write(r + "\n")
resultFyle.close()

I don't know if this solution is any better than the ones already offered, but it more closely reflects your original logic so I thought I'd share.

answered Sep 30, 2016 at 17:38

casscass

8181 gold badge10 silver badges16 bronze badges

1

A sample - write multiple rows with boolean column (using example above by GaretJax and Eran?).

import csv
RESULT = [['IsBerry','FruitName'],
          [False,'apple'],
          [True, 'cherry'],
          [False,'orange'],
          [False,'pineapple'],
          [True, 'strawberry']]
with open("../datasets/dashdb.csv", 'wb') as resultFile:
    wr = csv.writer(resultFile, dialect='excel')
    wr.writerows(RESULT)

Result:

df_data_4 = pd.read_csv('../datasets/dashdb.csv')
df_data_4.head()

Output:

   IsBerry  FruitName
   0    False   apple
   1    True    cherry
   2    False   orange
   3    False   pineapple
   4    True    strawberry

answered Feb 14, 2018 at 17:01

RaguRagu

313 bronze badges

How do I convert a string to a CSV file in Python?

Python Write CSV File.
First, open the CSV file for writing ( w mode) by using the open() function..
Second, create a CSV writer object by calling the writer() function of the csv module..
Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object..

How do I convert a list to a CSV file in Python?

To convert a list of lists to csv in python, we can use the csv. writer() method along with the csv. writerow() method.

How do I make a list into a csv file?

The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.

How do I write to column wise in a csv file in Python?

How to write a CSV file by column in Python.
list_1 = ["Hello", "World", "Monty", "Python"].
list_2 = [1, 2, 3, 4].
file = open("columns.txt", "w").
writer = csv. writer(file).
for w in range(4): iterate through integers 0-3..
writer. writerow([list_1[w], list_2[w]]).
file. close().