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)
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.
writerows() This function takes a list of iterables as parameter and writes each item as a comma separated line of items in the file.
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.
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 !']]
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.
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'])
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.
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!
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With