Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python list to csv file with each item in new line

Tags:

python

list

csv

I am trying to write a function that takes in a list of strings and writes each String in the list as a separate row in a csv file, but I am not getting any output. Could you please help me understand what I am doing wrong. Here is my code:

 import sys 
 import os
 import csv

 list= ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

 def write_to_csv(list_of_emails):
     with open('emails.csv', 'w', newline='') as csvfile:
         writer = csv.writer(csvfile, delimiter = ',')
         writer.writerows(list_of_emails)

write_to_csv(list)  
like image 929
marialadelbario Avatar asked Jun 29 '18 11:06

marialadelbario


People also ask

How do I convert a list into a csv file in python?

To convert the list to csv, we need to convert from list to dataframe and then use the to_csv() function to convert dataframe to a csv file. In this example, we have first imported pandas library and then define the four lists and map it with its column using a dictionary.

What method takes a list argument and writes it to a CSV file?

writerows() This function takes a list of iterables as parameter and writes each item as a comma separated line of items in the file.

How do I read all lines in a csv file in python?

Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method. Step 2: Create a reader object by passing the above-created file object to the reader function. Step 3: Use for loop on reader object to get each row.

How to write data from list to CSV file in Python?

The most common method to write data from a list to CSV file is the writerow () method of writer and DictWriter class. Creating a CSV file and writing data row-wise into it using writer class. data = [ ['Geeks'], [4], ['geeks !']]

How to get the values in the newline in CSV file?

And newline = ‘ ‘ is used to get the values in the newline. The csv.writer (file) is used to write all the data from the list to the CSV file, the write.writerows is used to write all the rows to the file.

How to write each row of the list to a CSV?

The writer.writerow is used to write each row of the list to a CSV file. The [‘grade’,’B’] is the new list which is appended to the existing file. import csv with open ('result.csv','a') as f: writer = csv.writer (f) writer.writerow ( ['grade','B'])

How do I append a list to a CSV file?

The mode “a” is used to append the file, and writer = csv.writer (f) is used to write all the data from the list to a CSV file. The writer.writerow is used to write each row of the list to a CSV file. The [‘grade’,’B’] is the new list which is appended to the existing file.


Video Answer


2 Answers

Why don't you try doing it with pandas instead. It's super easy. :)

lst = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

First, import package

import pandas

Then, create dataframe

df = pandas.DataFrame(data={"email": lst})

Then, export to csv :)

df.to_csv("./mycsv.csv", sep=',',index=False)

Done :) let me know if this one works for you!

like image 198
Ingrid Avatar answered Oct 15 '22 17:10

Ingrid


If your just writing each string on a seperate line, you can just keep it simple and just write each item with a \n:

lst= ['[email protected]', '[email protected]', '[email protected]']

def write_to_csv(list_of_emails):
    with open('emails.csv', 'w') as csvfile:
        for domain in list_of_emails:
            csvfile.write(domain + '\n')

write_to_csv(lst)

Which Outputs:

[email protected]
[email protected]
[email protected]

You also shouldn't use list as a variable name, since it shadows the builtin function list.

like image 21
RoadRunner Avatar answered Oct 15 '22 18:10

RoadRunner