使用 Python 3 & 写入文件统一码
这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的症状看起来像常规的“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/
看来 python 不理解你的源代码采用的编码。你必须通过使用字节顺序标记(首选)或通过在第一行或第二行中使用特殊注释声明类型来告诉它。因此:
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:
encoding: utf-8
.