我的csv作家中的for循环并没有将我的“标题”和“日期”并排放置

问题描述 投票:0回答:1

我正在尝试逐行输出每一篇文章的“标题”和“日期”,我从一个特定区域从网页上划分到csv。

所以我知道:

with open("Output.csv", "a") as f:
    wr = csv.writer(f)  

...

        wr.writerows([[title.text.strip()] for title in specific_area.findAll('h1')] + [[date.text.strip()] for date in specific_area.findAll('h6')])

这个输出首先为标题执行for循环,然后为之后的日期执行for循环,因此每个都在它们自己的行中。

如何让for循环同时完成?非常感谢!

python csv for-loop writer
1个回答
0
投票

尝试使用zip函数。

wr.writerows([[title.text.strip(), date.text.strip()] for title, date in zip(specific_area.findAll('h1'), specific_area.findAll('h6'))])

© www.soinside.com 2019 - 2024. All rights reserved.