2

I have a CSV file and when I try to read it in Python 2.7, it makes following error.

UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 10: ordinal not in range(128)

This is my code so far,

with open('train.csv', 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
    text=(row['Descript'])
    emotion = (row['Category'])

I found many solution from this site like How to fix: "UnicodeDecodeError: 'ascii' codec can't decode byte", UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1 But they are not able to fix my error. Can anyone help me.

7
  • 1
    Why are you reading the file in binary mode? Commented Jun 18, 2017 at 5:19
  • If i used 'r' instead of 'rb', it also make the same error. Commented Jun 18, 2017 at 5:20
  • this might help: stackoverflow.com/questions/23917729/… and: python2 or python3? then one or two of your offending input-lines might be helpful for people wanting to help. Commented Jun 18, 2017 at 5:36
  • 1
    could have been easier if you wrote the csv with str.encode('utf-8') Commented Jun 18, 2017 at 5:45
  • 2
    Use Python 3, if possible. If not possible, use it anyways. Or unicodecsv Commented Jun 18, 2017 at 5:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.