Python Error: UnicodeEncodeError: ‘charmap’ codec can’t encode character
I’ve discovered an error on your example ( https://www.example-code.com/chilkat2-python/xml_i.asp ) as follows…
AccountID: f1f489fb-9267-429e-a1dc-bc63a7c3f71e Traceback (most recent call last): File "M:\EXPORTIT\PY\CK_xero_accounts.py", line 50, in print("Name: " + xml.GetChildContent("Accounts|Account[i]|Name")) File "m:\Python\Python35-32\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 20: character maps to
It’s failing on the this line from the “https://www.chilkatsoft.com/exampleData/xero_accounts.xml”,
Wages Payable – Payroll
The problem is that your Python code is expecting the returned string to be in the cp437 byte representation. “cp437” is code page 437, which is:
Code page 437 is the character set of the original IBM PC (personal computer). It is also known as CP437, OEM-US, OEM 437, PC-8, or DOS Latin US. The set includes all printable ASCII characters, extended codes for accented letters (diacritics), some Greek letters, icons, and line-drawing symbols.
The problematic char is the funky dash char. Chilkat is returning strings using the utf-8 byte representation. The fix in Python is to tell Python that you’re expecting utf-8. See https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python or Google “python use utf-8 by default” to see what other possible solutions exist.
Specifically, add the following to the top of your Python script:
import sys import io sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')