在Python中为CSV输出附加列表 [英] Append lists for CSV output in Python

查看:112
本文介绍了在Python中为CSV输出附加列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在从Web上抓取数据,并希望将其输出为CSV. 一切工作正常,但是一旦我在迭代中追加多个列表,该列表的格式就会错误.

At the moment I am scraping data from the web and want to output it into CSV. Everything is working fine but as soon as I append more than one list in the iteration the list has the wrong format.

我从这样的事情开始:

list = [a, b, c]
list_two = [d, e, f]
list_three = [g, h, i]

第一次迭代:

list = [list, list_two]
# list = [[a, b, c], [d, e, f]]

第二次迭代:

list = [list, list_three]

我得到:

# list = [[[a, b, c], [d, e, f]], [g, h, i]]

我想要拥有:

# list = [[a, b, c], [d, e, f], [g, h, i]]

请帮助我!我想这是一件容易的事,但我不明白.而且我实际上很难找到有关如何添加列表的信息.

Please help me! I guess it is a easy thing but I do not get it. And I actually struggle to find information on how to append lists.

推荐答案

只需使用+连接两个列表:

Just use + to concatenate two lists :

list = [ list, list_two ]
list += [ list_three ]

您还可以使用append:

You could also use append :

list = [ list ]
list.append( list_two )
list.append( list_three )

这篇关于在Python中为CSV输出附加列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆