python write unicode to file easily? -


i want make sure string unicode in code, use unicode_literals, need write string file:

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

so need this:

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

but every time need encode in code, lazy, change codecs:

from __future__ import unicode_literals codecs import open import locale, codecs lang, encoding = locale.getdefaultlocale()  open('/tmp/test', 'wb', encoding) f:     f.write("中文") 

still think if want write file, easier method?

you don't need call .encode() , don't need call locale.getdefaultlocale() explicitly:

#!/usr/bin/env python # -*- coding: utf-8 -*- import io  io.open('/tmp/test', 'w') file:     f.write(u"中文" * 4) 

it uses locale.getpreferredencoding(false) character encoding save unicode text file.

on python 3:

  • you don't need use explicit encoding declaration (# -*- coding: utf-8 -*-), use literal non-ascii characters in python source code. utf-8 default.

  • you don't need use import io: builtin open() io.open() there

  • you don't need use u'' (u prefix). '' literals unicode default. if want omit u'' put from __future__ import unicode_literals in code in question.

i.e., complete python 3 code is:

#!/usr/bin/env python3  open('/tmp/test', 'w') file:     f.write("中文" * 4) 

Comments