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

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

    Actually, this worked for me for reading a file with UTF-8 encoding in Python 3.2:

    import codecs
    f = codecs.open('file_name.txt', 'r', 'UTF-8')
    for line in f:
        print(line)
    

提交回复
热议问题