Reading non-ASCII characters from a text file

前端 未结 3 1671
孤独总比滥情好
孤独总比滥情好 2021-01-06 00:03

I\'m using python 2.7. I\'ve tried many things like codecs but didn\'t work. How can I fix this.

myfile.txt

wörd

My code

         


        
3条回答
  •  心在旅途
    2021-01-06 00:40

    It's the terminal encoding. Try to configure your terminal with the same encoding you are using in your file. I recomend you to use UTF-8.

    By the way, is a good practice to decode-encode all your inputs-outputs to avoid problems:

    f = open('test.txt','r')    
    for line in f:
        l = unicode(line, encoding='utf-8')# decode the input                                                                                  
        print l.encode('utf-8') # encode the output                                                                                            
    f.close()
    

提交回复
热议问题