python list to csv file with each item in new line

后端 未结 5 543
无人及你
无人及你 2021-01-19 02:10

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 yo

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 03:04

    You need to use writerow

    Ex:

    import sys 
    import os
    import csv
    
    l= ['name@domain.com', 'name@domain.com', 'name@domain.com', 'name@domain.com', 'name@domain.com', 'name@domain.com']
    
    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)  
    

提交回复
热议问题