Python – 奇怪的UnicodeEncodeError

huangapple go评论67阅读模式
英文:

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(&quot;~&quot;) + &quot;/EF5/LIB&quot;)

import EF # Irrelevant for the purpose of this test. Though removing it did not help!

print(&quot;Content-Type: text/plain;charset=utf-8\n&quot;)

def tester(n):
	b = n.encode(&#39;utf-8&#39;)
	s = b.decode(&#39;utf-8&#39;)
	return s + &quot;!&quot;

try:
	print(&quot;Hello 高欧凡&quot;)

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 高欧凡")

英文:

try something like this

import io
import sys

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding=&#39;utf-8&#39;)

print(&quot;Hello 高欧凡&quot;)

For example:

Python – 奇怪的UnicodeEncodeError

huangapple
  • 本文由 发表于 2023年6月29日 09:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定