Encoding/decoding: Still don't get it :-/

Gilles Ganault nospam at nospam.com
Fri Mar 13 13:56:48 CET 2009


I must be dense, but I still don't understand 1) why Python sometimes
barfs out this type of error when displaying text that might not be
Unicode-encoded, 2) whether I should use encode() or decode() to solve
the issue, or even 3) if this is a Python issue or due to APWS SQLite
wrapper that I'm using:

sql = 'SELECT id,address FROM companies'

for row in rows:
	id = row[0]

	#could be 'utf-8', 'iso8859-1' or 'cp1252'
		address = row[1]
	except UnicodeDecodeError:
			address = row[1].decode('iso8859-1')
		except UnicodeDecodeError:
			address = row[1].decode('cp1252')

	print id,address
152 Traceback (most recent call last):
  File "C:\zip.py", line 28, in <module>
    print id,address
  File "C:\Python25\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xc8' in
position 2
4: character maps to <undefined>

Thank you for any tip.

More information about the Python-list mailing list