我正在尝试编写一个函数,该函数接受字符串列表并将列表中的每个字符串作为单独的行写入 csv 文件中,但我没有得到任何输出。你能帮我理解我做错了什么吗?这是我的代码:
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)
你为什么不尝试这样做pandas反而。这非常简单。 :)
lst = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
一、导入包
import pandas
然后,创建数据框
df = pandas.DataFrame(data={"email": lst})
然后,导出到 csv :)
df.to_csv("./mycsv.csv", sep=',',index=False)
完成 :) 让我知道这个是否适合您!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)