(Jun-28-2018, 02:01 AM)Skaperen Wrote: UnicodeEncodeError: 'ascii' codec can't encode characterThis show that your Terminal/OS setup is the problem or maybe call Python 2,i think i mention this before to you.
You should not get
UnicodeEncodeError: 'ascii'
in Python 3,butUnicodeEncodeError: 'utf-8'
if problem with encoding.Quote:The default encoding for Python 3 source code is UTF-8.
The default encoding for Python 2 source code is ASCII.
mint@mint ~ $ python3 Python 3.6.4 (default, Mar 15 2018, 15:35:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> s = '\xa9' >>> s '©' >>> print(s) © >>> b = b'\xa9' >>> b b'\xa9' Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 0: invalid start byte >>> print(b.decode('latin')) © >>>Test:
python3 -c "import sys; print(sys.stdout.encoding)" UTF-8 mint@mint ~ $ python3 -c "print('Spicy jalapeño ☂')" Spicy jalapeño ☂ mint@mint ~ $ python3 Python 3.6.4 (default, Mar 15 2018, 15:35:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import locale >>> locale.getdefaultlocale() ('en_US', 'UTF-8') >>> locale.getpreferredencoding() 'UTF-8' >>> exit()locale:
mint@mint ~ $ locale LANG=en_US.UTF-8 LANGUAGE= LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=Read write:
# uni.py uni = '©' with open('uni.txt', 'w', encoding='utf-8') as f_out: f_out.write(uni) with open('uni.txt', encoding="utf-8") as f: print(f.read())
Output:mint@mint ~ $ python3 uni.py
©