Unicode (UTF-8) reading and writing to files in Python

前端 未结 14 1221
谎友^
谎友^ 2020-11-22 17:10

I\'m having some brain failure in understanding reading and writing text to a file (Python 2.4).

# The string, which has an a-acute in it.
ss = u\'Capit\\xe1         


        
14条回答
  •  天涯浪人
    2020-11-22 17:21

    Rather than mess with the encode and decode methods I find it easier to specify the encoding when opening the file. The io module (added in Python 2.6) provides an io.open function, which has an encoding parameter.

    Use the open method from the io module.

    >>>import io
    >>>f = io.open("test", mode="r", encoding="utf-8")
    

    Then after calling f's read() function, an encoded Unicode object is returned.

    >>>f.read()
    u'Capit\xe1l\n\n'
    

    Note that in Python 3, the io.open function is an alias for the built-in open function. The built-in open function only supports the encoding argument in Python 3, not Python 2.

    Edit: Previously this answer recommended the codecs module. The codecs module can cause problems when mixing read() and readline(), so this answer now recommends the io module instead.

    Use the open method from the codecs module.

    >>>import codecs
    >>>f = codecs.open("test", "r", "utf-8")
    

    Then after calling f's read() function, an encoded Unicode object is returned.

    >>>f.read()
    u'Capit\xe1l\n\n'
    

    If you know the encoding of a file, using the codecs package is going to be much less confusing.

    See http://docs.python.org/library/codecs.html#codecs.open

提交回复
热议问题