python list to csv file with each item in new line

后端 未结 5 647
无人及你
无人及你 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 02:56

    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 = ['name1@domain.com', 'name2@domain.com', 'name3@domain.com']
    
    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)
    

提交回复
热议问题