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

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

    You can also improve the original open() function to work with Unicode files by replacing it in place, using the partial function. The beauty of this solution is you don't need to change any old code. It's transparent.

    import codecs
    import functools
    open = functools.partial(codecs.open, encoding='utf-8')
    

提交回复
热议问题