Reading non-ASCII characters from a text file

前端 未结 3 1794
孤独总比滥情好
孤独总比滥情好 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:43

    1. First of all - detect the file's encoding
    
      from chardet import detect
      encoding = lambda x: detect(x)['encoding']
      print encoding(line)
    
    1. then - convert it to unicode or your default encoding str:
    
      n_line=unicode(line,encoding(line),errors='ignore')
      print n_line
      print n_line.encode('utf8')
    

提交回复
热议问题