Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

sublimetext - Why does the Python print statement cause a UnicodeEncodeError?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No matter which editor you use or which OS you use, this will always work. simply using sys.stdout.buffer.write() which is one of the best practices.

import sys 

s2 = '∞?????????????????????'
s3 = 'α?áà????éèêíì??óò???úùü?'
s4 = '????│▏┃┆┇┊?┋???????????┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛'

x = s2.encode('utf8')
y = s3.encode('utf8')
z = s4.encode('utf8')

# you can use loops if you want, python is fun!
sys.stdout.buffer.write(x)
print("")
sys.stdout.buffer.write(y)
print("")
sys.stdout.buffer.write(z)
print("")

output:

∞?????????????????????
α?áà????éèêíì??óò???úùü?
????│▏┃┆┇┊?┋???????????┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...