保存为刮出的输出

2024-09-24 10:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

从rss提要中,我可以从每个提要中提取内容,但现在我尝试将每个输出保存到单独的txt文件中,并使用它们的标题,例如newstTitle1.txt、newstTitle2.txt……但到目前为止,使用下面的代码,它只保存列表中的最后一个输出。有没有办法解决这个问题

with open('textfile.txt', 'w') as handle:
        handle.write(text)
    counter+=1

Tags: 文件代码txt标题内容列表aswith
1条回答
网友
1楼 · 发布于 2024-09-24 10:28:48
# somewhere in code

counter = 0

# in place where you save it

with open('textfile' + str(counter) + '.txt', 'w') as handle:
    handle.write(text)
counter+=1

或者如果变量newstitle中有标题

with open(newstitle + '.txt', 'w') as handle:
    handle.write(text)

注意文件名中的本地字母、空格和其他奇怪字符

相关问题 更多 >