英文:
Python - wierd UnicodeEncodeError
问题
I am using Python 3.6 on a CentOS server, and I am trying to write a script outside the scope of Django (though it is installed).
All the script needs to do is some kind of logging, but it may have Unicode characters in it.
The issue is no matter what solution I try, this simply pops up the same error:
<class 'UnicodeEncodeError'>
'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)
My code (this is just a tester file):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, os, traceback, time
sys.path.append(os.path.expanduser("~") + "/EF5/LIB")
import EF # Irrelevant for the purpose of this test. Though removing it did not help!
print("Content-Type: text/plain;charset=utf-8\n")
def tester(n):
b = n.encode('utf-8')
s = b.decode('utf-8')
return s + "!"
try:
print("Hello 高欧凡")
except Exception as err:
print(type(err))
print(err)
print(traceback.format_exc())
I have tried to use print(tester("Hello 高欧凡"))
to encode/decode, forcing utf-8.
I have tried leaving it as bytes-array (the output is b'..\x..' etc).
I have tried environmental variable PYTHONIOENCODING="utf-8"
.
I have tried sys.stdin.encoding="utf-8"
(gives error!).
I have tried sys.stdout.encoding="utf-8"
(gives error!).
I have tried import codecs
and use this (the same behavior as of str in my case).
Is there any other solution....?
From where the hell this "ascii" codec arrived, if ALL my settings point to utf-8, and Python 3 uses utf-8 by default???
I cannot access configuration; I need an in-program solution.
Thanks a lot.
英文:
I am using Python 3.6 in centos server, and I am trying to write a script outside the scope of django (though it is installed).
All the script need to do is some kind of logging, but it may have unicode characters in it.
The issue is no matter what solution I will try, this simply popup the same error:
> <class 'UnicodeEncodeError'><br>
> 'ascii' codec can't encode characters in position 6-8: ordinal not in range(128)
My code (this is just a tester file):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, os, traceback, time
sys.path.append(os.path.expanduser("~") + "/EF5/LIB")
import EF # Irrelevant for the purpose of this test. Though removing it did not help!
print("Content-Type: text/plain;charset=utf-8\n")
def tester(n):
b = n.encode('utf-8')
s = b.decode('utf-8')
return s + "!"
try:
print("Hello 高欧凡")
except Exception as err:
print(type(err))
print(err)
print(traceback.format_exc())
I have tried to use print(tester("Hello 高欧凡")) so to encode/decode forcing utf-8.<br>
I have tried leaving it bytes-array (the output is b'\x..\x..' etc)<br>
I have tried environmental variable PYTHONIOENCODING="utf-8"<br>
I have tried sys.stdin.encoding="utf-8" (gives error!)<br>
I have tried sys.stdout.encoding="utf-8" (gives error!)<br>
I have tried import codecs and use this (the same behaviour as of str in my case).<br>
Is there any other colution....?
From where the hell this "ascii" codec arrived, if ALL my settings points to utf-8, and Python 3 using utf-8 by default???
I cannot access configuration, though, I need an in-program solution.
Thanks a lot
答案1
得分: 1
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
print("Hello 高欧凡")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论