gpt4 book ai didi

Python脚本需要简单修改才能在文件中打印

转载 作者:太空宇宙 更新时间:2023-11-03 16:55:03 25 4
gpt4 key购买 nike

正如我之前在 Perl 上编写的代码一样,我没有 Python 经验,因此我会请求一些帮助来修改我的文件。

我有一个脚本,可以从 URL 获取文本并保存多个 html。如何修改脚本以将内容写入一个大的 HTML 而不是许多单独的 html 文件?

我有:

for a in article.findAll('a'):
a.unwrap()

with open(news_folder + '/' + news_file, "w") as f:
print("<!DOCTYPE html>", file=f)
print("<html lang='ja'>", file=f)
print("<head><meta charset='utf-8'></head>", file=f)
print("<style>p { font-size: 120%; line-height: 3.2; padding-bottom: 20px; }</style>", file=f)
print("<body>", file=f)
print(title, file=f)
print(article, file=f)
print("</body>", file=f)
print("</html>", file=f)

最佳答案

不要对文件使用print()write()。如果您打算对同一个文件使用不同的数据多次执行此操作,请以 append 模式打开该文件:

with open(news_folder + '/single_output.html', "a") as f:
f.write("<!DOCTYPE html>")
f.write("<html lang='ja'>")
#etc.

关于Python脚本需要简单修改才能在文件中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com