Python Forum

Full Version: Issue with special characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to use special characters such as ▒ and █ in my code, and save these to a text file.
However, I am getting an encoding error:
Error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2588' in position 2: character maps to <undefined>
I have seen so many topics on this, but they are all really confusing. Wall Is there a simple way of dealing with this? Thanks!
Windows? The Windows console doesn't support Unicode. As I know. I don't use Windows at all though. See this. It may help.
This is for Python 3.6,not Python 2.
with open('foo.txt', 'w', encoding='utf-8') as f_out:
    s = '▒ █'
    f_out.write(s)

with open('foo.txt', encoding='utf-8') as f:
    print(f.read())
cmder as i use,do i except to print correct result,but also cmd do print correct.
cmd output:
E:\1py_div>python uni1.py
▒ █