1

I am getting the following Exception when trying to save a row to a db:

Unexpected error: (<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('latin-1', u"First 'A\u043a' Last", 7, 8, 'ordinal not in range(256)'), <traceback object at 0x106562908>)

Before inserting, I am converting every string in a dictionary to latin-1 like this:

for k,v in row.items():
    if type(v) is str:
        row[k] = v.decode('utf-8').encode('latin-1')

The offending character seems to be 'A\u043a' - in other cases there seem to be other characters also "not within range."

Help appreciated.

1 Answer 1

1

Solved. The problem was attempting to decode a string which was already UTF-8. I also added 'ignore' to the encode() arguments,

v.encode('latin-1', 'ignore')

This ensures any non-encodable characters are replaced with a '?'

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.