Issue
I've some problem with decode
method in python 3.3.4. This is my code:
for line in open('file','r'):
decodedLine = line.decode('ISO-8859-1')
line = decodedLine.split('\t')
But I can't decode the line for this problem:
AttributeError: 'str' object has no attribute 'decode'
Do you have any ideas? Thanks
Solution
One encodes strings, and one decodes bytes.
You should read bytes from the file and decode them:
for lines in open('file','rb'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
Luckily open
has an encoding argument which makes this easy:
for decodedLine in open('file', 'r', encoding='ISO-8859-1'):
line = decodedLine.split('\t')
Answered By - Veedrac
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.