8

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)  

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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!

Comments

2

You can use delimiter='\n' with csv.writer.writerow (notice, not writerows). In addition, newline='' is not required as an argument to open.

import sys 
import os
import csv

L = ['[email protected]', '[email protected]', '[email protected]']

def write_to_csv(list_of_emails):
    with open('emails.csv', 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter='\n')
        writer.writerow(list_of_emails)

write_to_csv(L)

Comments

0

You need to use writerow

Ex:

import sys 
import os
import csv

l= ['[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 = '\n')
        writer.writerow(list_of_emails)

write_to_csv(l)  

Comments

-1

You can change the code as follows:

import sys 
import os
import csv

listOfLines= [['[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(listOfLines)

The function writer.writerows() takes a list of lists, in our case [['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']], where every sub-list inside the original list is treated as a single line of the CSV file. You also should not use reserved keywords like list for variable names. list,dict are functions used to create Lists and Dictionaries respectively. May be in your code it did not throw any error, but for scripts that are larger than the current piece of code in question, the interpreter throws errors for the same and such errors become harder to debug

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.