目前我有一个填充列表
list =[a, b, c, d]
我想将列表的内容转换为csvfile,并使列表的所有元素都成为一列。这是我的当前代码:
with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)
输出的csv文件包含列表的元素,但正如方法所暗示的,它已将所有内容写入第一行。你知道吗
我试过创建for循环来编写每个element + \n
,但是writerow
方法有一个问题(在每个字母后面加逗号),而且csvwriter
没有writecolumn
方法。你知道吗
您可以将列表中的元素连接到一个字符串中,并使用新行字符
'\n\r'
作为分隔符,然后将整个字符串写入文件。你知道吗例如:
(也是
'\n'
工程)要执行此任务,我将使用^{} 包,如下所示:
pandas
阅读csv文件甚至处理Excel文件也很好。你知道吗这是我能想到的最简单的方法:
注意,我将list变量的名称更改为
my_list
,这样它就不会与内置的list
类冲突。你知道吗相关问题 更多 >
编程相关推荐