2

Trying to read from a CSV file and write the data into an XML file. I am encountering:

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

My question is, what is the best way to ignore this kind of error and continue processing the data set. After reading other similar questions, I did add: # -*- coding: utf-8 -*- to my file but it didn't help

1
  • Properly decode the input, e.g. read as bytes and then do input.decode("utf-8") (if your input is utf-8). Commented Sep 8, 2016 at 15:01

2 Answers 2

1

You can try opening csv with codecs:

import codecs
codecs.open(file_name, 'r', 'utf8')

Given that each line will contain '\n' string you will need to apply line.rstrip() when looping trough lines.

Note: Please don't try to convert values to str as you will encounter another error there.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Boris, going to try your suggestion
Please read the edit, that '\n' gave me headaches more than once :)
This is how I am reading at the moment: with open('myFile.csv', 'rb') as ifile: reader = csv.reader(ifile) for rownum, row in enumerate(reader):
You can first replace open inside your with statement with codecs.open as i suggested in answer. Then just use for rownum, row in enumerate(ifile): row = row.rstrip() on the first line of your iteration and it should replace your csv.reader() method.
1

I was getting this error while reading readme ad long description in setup.py. If you are using open, you can use the encoding parameter:

with open("README.md", "r", encoding='utf_8') as f:
    long_description = f.read()

1 Comment

integrated python 2.7 open does not have encoding param.

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.