'str' object has no attribute 'decode' in Python3

后端 未结 3 760
北恋
北恋 2020-12-01 18:59

I\'ve some problem with \"decode\" method in python 3.3.4. This is my code:

for lines in open(\'file\',\'r\'):
    decodedLine = lines.decode(\'ISO-8859-1\')         


        
3条回答
  •  盖世英雄少女心
    2020-12-01 19:44

    This works for me smoothly to read Chinese text in Python 3.6. First, convert str to bytes, and then decode them.

    for l in open('chinese2.txt','rb'):
        decodedLine = l.decode('gb2312')
        print(decodedLine)
    

提交回复
热议问题