如何轻松地使用Python将Unicode写入文件?

6

我希望在我的代码中确保所有字符串都是unicode格式的,所以我使用了unicode_literals,然后我需要将字符串写入文件:

from __future__ import unicode_literals
with open('/tmp/test', 'wb') as f:
    f.write("中文") # UnicodeEncodeError

所以我需要做这个:

from __future__ import unicode_literals
with open('/tmp/test', 'wb') as f:
    f.write("中文".encode("utf-8"))
    f.write("中文".encode("utf-8"))
    f.write("中文".encode("utf-8"))
    f.write("中文".encode("utf-8"))

但每次我需要编码时,都感到很懒,所以我改用codecs:

from __future__ import unicode_literals
from codecs import open
import locale, codecs
lang, encoding = locale.getdefaultlocale()

with open('/tmp/test', 'wb', encoding) as f:
    f.write("中文")

如果我只想将内容写入文件,那么这样做是否过于繁琐?有没有更简单的方法?


2个回答

4

您无需调用.encode(),也无需显式调用locale.getdefaultlocale()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import io

with io.open('/tmp/test', 'w') as file:
    file.write(u"中文" * 4)

它使用locale.getpreferredencoding(False)字符编码来将Unicode文本保存到文件中。
在Python 3中:
  • 您不需要使用显式的编码声明(# -*- coding: utf-8 -*-)来在Python源代码中使用字面非ASCII字符。默认为utf-8

  • 您不需要使用import io:内置的open()就是io.open()

  • 您不需要使用u''u前缀)。默认情况下,''字面量是Unicode。如果要省略u'',则请像问题中的代码一样重新添加from __future__ import unicode_literals
即完整的Python 3代码为:
#!/usr/bin/env python3

with open('/tmp/test', 'w') as file:
    file.write("中文" * 4)

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接