简体   繁体   English

Python作家很麻烦

[英]Python writerows trouble

I begin in Python language and face a problem with my script. 我从Python语言开始,面对我的脚本问题。 On stackoverflow, i fould only a post where somebody asks how to write in columns, but form me it is the problem :) 在stackoverflow上,我只能发布一个帖子,有人问如何写入列,但形成我是问题:)

I have two lists. 我有两个清单。 The first one contains some other lists which content must be written in a csv file, the second one the name of the files to be created. 第一个包含一些其他列表,其中的内容必须写在csv文件中,第二个列表是要创建的文件的名称。

contentlist = [element1, element2 ... ]
namelist = ['element1', 'element2' ... ]

Each item of the list contentlist follows this model : 列表内容列表的每个项目都遵循以下模型:

'url 1 | text1'

For output i would like something like this, url and text being divided by the | 对于输出我想要这样的东西,url和文本被|分开 character : 性格:

Column1          Column2

url1             text1

url2             text2

My script is partially successful. 我的脚本部分成功。

    for name, content in zip(namelist, contentlist):
        with open(name, 'w') as f:
        w = csv.writer(f, dialect = 'excel', delimiter=',')
        w.writerows([content]) 

But I get this output : 但我得到这个输出:

column1         column2

url1 | text1    url2 | text2

If i delete [ ] at the last line of my script, i manage ton get the output i'm wanting, but obviously there is a comma between each character. 如果我在我的脚本的最后一行删除[],我管理吨得到我想要的输出,但显然每个字符之间有一个逗号。

Could someone help me ? 有人能帮助我吗? Thanks in advance. 提前致谢。

w.writerows(c.split(' | ') for c in content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM