使用 Python 3 & 写入文件统一码

发布于 2024-10-16 18:02:54 字数 1218 浏览 10 评论 0原文

这是我的代码:

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()

但我给了我:

一份声明公平吗?是为了鼓励吗?巴黎圣母院旅 d'élite 击败特工 est lé pour vous aider。上诉的理由 上诉人 06 立即给予您鼓励。氮 赫斯特兹海峡让您的助手杜兰特晚会,瞧 接受测试,成功:

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

Here is my code:

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()

But I gives me:

Une déclaration à faire ? Besoin d'encouragements? Notre brigade
d'élite beat agent est là pour vous aider. Faites appel à nous en
appelant le 06 et nous accourrons vous encourager dans l'instant. N
hésitez pas. Et pour vous aider durant cette soirée, voilà une
accroche a tester, succès garanti :

So how to write in a file with unicode string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-10-23 18:02:54

您的症状看起来像常规的“UTF-8 as latin-1”问题。

您是否检查过用于查看该文件的软件使用了什么编码?我想说问题不一定出在你的 python 代码中,而是出在查看器中。

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

另外,在 python 3 中,默认源编码是 UTF-8。 http://www.python.org/dev/peps/pep-3120/

Your symptoms look like regular a "UTF-8 as latin-1" problem.

Have you checked what encoding is used on the software that you are using to view that file? I'd say that the problem is not necessarily in your python code but in the viewer.

If you create a file with your example text Une déclaration à faire... using UTF-8 encoding and then read that file interpreting the contents using encoding ISO-8859-1 or windows-1252, then the result is shown as the output you described: Une déclaration à faire....

Also, in python 3 the default source encoding is UTF-8. http://www.python.org/dev/peps/pep-3120/

木槿暧夏七纪年 2024-10-23 18:02:54

看来 python 不理解你的源代码采用的编码。你必须通过使用字节顺序标记(首选)或通过在第一行或第二行中使用特殊注释声明类型来告诉它。因此:

  1. 告诉您的编辑器在源代码中存储字节顺序标记,或者,如果不能这样做,
  2. 请在开头添加包含 (IIRC) encoding: utf-8 的注释。

It seems python does not understand what encoding your source is in. You have to tell it, either by using byte-order mark (preferred) or via declaring the type with special comment in first or second line. So:

  1. tell your editor to store byte-order-mark in the source, or, if it can't do that
  2. put a comment at the beginning containing (IIRC) encoding: utf-8.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文