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

前端 未结 14 1056
谎友^
谎友^ 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:26

    Now all you need in Python3 is open(Filename, 'r', encoding='utf-8')

    [Edit on 2016-02-10 for requested clarification]

    Python3 added the encoding parameter to its open function. The following information about the open function is gathered from here: https://docs.python.org/3/library/functions.html#open

    open(file, mode='r', buffering=-1, 
          encoding=None, errors=None, newline=None, 
          closefd=True, opener=None)
    

    Encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

    So by adding encoding='utf-8' as a parameter to the open function, the file reading and writing is all done as utf8 (which is also now the default encoding of everything done in Python.)

提交回复
热议问题