Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for lin in openfile:
#7
(Jun-28-2018, 02:01 AM)Skaperen Wrote: UnicodeEncodeError: 'ascii' codec can't encode character
This 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 ©
Reply


Messages In This Thread
for lin in openfile: - by Skaperen - Jun-27-2018, 03:41 AM
RE: for lin in openfile: - by buran - Jun-27-2018, 06:50 AM
RE: for lin in openfile: - by snippsat - Jun-27-2018, 09:14 AM
RE: for lin in openfile: - by Skaperen - Jun-27-2018, 10:02 PM
RE: for lin in openfile: - by snippsat - Jun-27-2018, 10:48 PM
RE: for lin in openfile: - by Skaperen - Jun-28-2018, 02:01 AM
RE: for lin in openfile: - by snippsat - Jun-28-2018, 12:47 PM
RE: for lin in openfile: - by DeaD_EyE - Jun-28-2018, 02:19 PM
RE: for lin in openfile: - by Skaperen - Jun-28-2018, 08:53 PM
RE: for lin in openfile: - by snippsat - Jun-28-2018, 09:42 PM
RE: for lin in openfile: - by Skaperen - Jun-28-2018, 11:36 PM
RE: for lin in openfile: - by snippsat - Jun-29-2018, 07:52 AM
RE: for lin in openfile: - by Skaperen - Jun-29-2018, 09:59 PM
RE: for lin in openfile: - by snippsat - Jun-30-2018, 12:07 PM
RE: for lin in openfile: - by Skaperen - Jun-30-2018, 05:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  combobox_callback(choice choice part of openfile name (kind of dependency) janeik 9 1,684 Sep-10-2023, 10:27 PM
Last Post: janeik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020