gpt4 book ai didi

python - 使用python将文本文件转换为html文件

转载 作者:太空狗 更新时间:2023-10-29 15:25:55 26 4
gpt4 key购买 nike

我有一个文本文件,其中包含:

JavaScript              0
/AA 0
OpenAction 1
AcroForm 0
JBIG2Decode 0
RichMedia 0
Launch 0
Colors>2^24 0
uri 0

我写了这段代码来将文本文件转换为 html:

contents = open("C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
for lines in contents.readlines():
e.write(lines + "<br>\n")

但我在 html 文件中遇到的问题是,在每一行中,两列之间没有空格:

JavaScript 0
/AA 0
OpenAction 1
AcroForm 0
JBIG2Decode 0
RichMedia 0
Launch 0
Colors>2^24 0
uri 0

我应该怎么做才能像文本文件中那样拥有相同的内容和两列

最佳答案

只需更改您的代码以包含 <pre></pre>标签,以确保您的文本的格式与您在原始文本文件中设置的格式保持一致。

contents = open"C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
for lines in contents.readlines():
e.write("<pre>" + lines + "</pre> <br>\n")

关于python - 使用python将文本文件转换为html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24715027/

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