File 使用Python 3&;编写文件;统一码

File 使用Python 3&;编写文件;统一码,file,unicode,python-3.x,encode,File,Unicode,Python 3.x,Encode,这是我的密码: import codecs filename = "worst.txt" file = open(filename, "r",encoding='utf-8') lines = file.readlines() texte = "" for line in lines: print(line) texte += line file.close() print(texte) texte = texte.split(";") print(texte) filename

这是我的密码:

import codecs
filename = "worst.txt"
file = open(filename, "r",encoding='utf-8')
lines = file.readlines()
texte = ""
for line in lines:
    print(line)
    texte += line
file.close()
print(texte)
texte = texte.split(";")
print(texte)
filename = "oudean.html"



file = open(filename, "w",encoding='utf-8')



file.write("<html><body>\r\n")
for t in texte :
        print(t)
        file.write("""<img src="ouedan.jpg"><br>\r\n""")
        file.write("""Une déclaration à faire ?<br>Besoin d'encouragements?<br>Notre brigade d'élite beat agent est là pour vous aider.<br>Faites appel à nous en appelant le  06 et nous accourrons vous encourager dans l'instant.<br>N hésitez pas.<br>Et pour vous aider durant cette soirée, voilà une accroche a tester, succès garanti :<br>%s<br><br><br><br><br><br><br><br>"""%t)
file.write("</body></html>\r\n")
file.close()
导入编解码器
filename=“最差的.txt”
文件=打开(文件名为“r”,编码为“utf-8”)
lines=file.readlines()
texte=“”
对于行中的行:
打印(行)
texte+=行
file.close()文件
打印(文本)
texte=texte.split(“;”)
打印(文本)
filename=“oudean.html”
文件=打开(文件名为“w”,编码为“utf-8”)
文件。写入(“\r\n”)
对于文本中的t:
打印(t)
文件。写入(“
\r\n”) file.write(“”)"公平吗?
是鼓励吗?
诺特旅的打击特工是你的助手。
出现在你眼前的信仰和鼓励你的理由。
N hésitez pas。
你的助手是杜兰特·塞特·索伊,你是一个测试者,成功的加兰蒂:





“”%t) 文件。写入(“\r\n”) file.close()文件
但我告诉我:

不,不,不,不,不,不,不,不,不,不,不,不,不,不,不 d'195©lite beat探员给你的助手。Faites appelÃnous en 请立即通知我们鼓励您的理由 你的助手杜兰特·塞特·索耶,瞧 accroche a测试仪,成功加兰蒂:


那么,如何使用unicode字符串写入文件?

python似乎不了解源代码的编码方式。您必须告诉它,或者使用字节顺序标记(首选),或者在第一行或第二行声明带有特殊注释的类型。因此:

  • 告诉编辑器在源代码中存储字节顺序标记,或者,如果它不能这样做
  • 在开头添加一条注释,其中包含(IIRC)
    编码:utf-8

  • 你的症状看起来像一个“UTF-8拉丁-1”问题

    您是否检查了用于查看该文件的软件上使用的编码?我认为问题不一定在python代码中,而是在查看器中

    如果您使用UTF-8编码创建一个示例文本
    Une déclarationáfaire…
    ,然后使用编码ISO-8859-1或windows-1252读取该文件解释内容,则结果显示为您描述的输出:
    Une déclarationÃfaire…

    此外,在Python3中,默认的源代码编码是UTF-8