Why am I getting an error trying to print a string in Python 2.7? -
i'm trying use
https://pypi.python.org/pypi/dbf
to read database file. i'm trying print records follows:
for record in race_db: print record this gives me error message
unicodedecodeerror: 'ascii' codec can't decode byte 0xc9 in position 4: ordinal not in range(128)
so tried map unicode
string_record = [unicode(item) item in record] same thing. utf-8:
string_record = [unicode(item, "utf8") item in record] typeerror: coercing unicode: need string or buffer, datetime.date found
i want sort tostring functionality doing wrong? know can loop through records because like:
print 'blah' works fine. it's in encoding tripping me up.
the single byte \xc9 not valid encoding of in utf-8. need figure out how text encoding—in case, it's either iso 8859-1 or windows-1252. then, decode accordingly.
for example, if it's windows-1252, you'd decode follows:
string_record = 'foo \xc9 bar' print string_record.decode('windows-1252') # output: "foo É bar"
Comments
Post a Comment