Rahul99
Rahul99

Reputation: 85

error :: UnicodeDecodeError

I am getting

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

I am using intgereproparty, stringproparty, datetimeproparty

Upvotes: 1

Views: 3099

Answers (4)

yurisich
yurisich

Reputation: 7119

When I write my foreign language "flashcard" programs, I always use python 3.x as its native encoding is utf-8. You're encoding problems will generally be far less frequent.

If you're working on a program that many people will share, however, you may want to consider using encode and decode with python 2.x, but only when storing and retrieving data elements in persistent storage. encode your non-ASCII characters, silently manipulate hexadecimal representations of those unicode strings in memory, and save them as hexadecimal. Finally, use decode when fetching unicode strings from persistant storage, but for end user display only. This will eliminate the need to constantly encode and decode your strings in your program.

@jcoon also has a pretty standard response to this problem.

Upvotes: 0

Wander Nauta
Wander Nauta

Reputation: 19615

You are trying to put Unicode data (probably text with accents) into an ASCII string.

You can use Python's codecs module to open a text file with UTF-8 encoding and write the Unicode data to it.

The .encode method may also help (u"õ".encode('utf-8') for example)

Upvotes: 1

Jason Coon
Jason Coon

Reputation: 18431

Python defaults to ASCII encoding - if you are dealing with chars outside of the ASCII range, you need to specify that in your code.

One way to do this is setting the defining the encoding at the top of your code.

This snippet sets the encoding at the top of the file to encoding to Latin-1 (which includes 0xb0):

#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
...

See PEP for more info on encoding.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328594

That's because 0xb0 (decimal 176) is not a valid character code in ASCII (which defines only values between 0 and 127).

Check where you got that string from and use the proper encoding.

If you need further help, post the code.

Upvotes: 1

Related Questions