Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ubuntu';使用Python脚本创建终端_Python_Unicode_Utf 8_Character Encoding_Terminal - Fatal编程技术网

在Ubuntu';使用Python脚本创建终端

在Ubuntu';使用Python脚本创建终端,python,unicode,utf-8,character-encoding,terminal,Python,Unicode,Utf 8,Character Encoding,Terminal,在我在Ubuntu命令行上运行的Python脚本中,它从MySQL数据库中选择UTF8编码的内容 然后,我想将字符串打印到控制台 显示的字符串存在编码问题,因为它们不能正确显示重音字符。我该如何解决这个问题 最好由脚本做出决定,而不是设置系统环境,以确保在其他系统上轻松运行。您可以像下面这样获得标准输出的当前编码: >>> import sys >>> sys.stdout.encoding UTF-8 #!/usr/bin/env python3.2 #

在我在Ubuntu命令行上运行的Python脚本中,它从MySQL数据库中选择UTF8编码的内容

然后,我想将字符串打印到控制台

显示的字符串存在编码问题,因为它们不能正确显示重音字符。我该如何解决这个问题


最好由脚本做出决定,而不是设置系统环境,以确保在其他系统上轻松运行。

您可以像下面这样获得标准输出的当前编码:

>>> import sys
>>> sys.stdout.encoding
UTF-8
#!/usr/bin/env python3.2
# -*- coding: UTF-8 -*-

from __future__ import print_function
from __future__ import unicode_literals

import re
import sys
import os

if not (("PYTHONIOENCODING" in os.environ)
            and
        re.search("^utf-?8$", os.environ["PYTHONIOENCODING"], re.I)):
    sys.stderr.write(sys.argv[0] + ": Please set your PYTHONIOENCODING envariable to utf8\n")
    sys.exit(1)

import unicodedata
if unicodedata.unidata_version < "6.0.0":
    print("WARNING: Your old UCD is out of date, expected at least 6.0.0 but got", 
           unicodedata.unidata_version)

wide_enough = (sys.maxunicode >= 0x10FFFF)
if not wide_enough:
    print("WARNING: Narrow build detected, your Python lacks full Unicode support!!")
然后对Unicode字符串进行相应的编码:

>>> u"Ä"
u'\xc4'
>>> sys.stdout.write(u"Ä".encode(enc, 'replace'))

当一个字符在终端的编码中不可表示时,可以避免UnicodeEncodeErrors。这将被一个问号代替。

您可以得到标准输出的当前编码,如下所示:

>>> import sys
>>> sys.stdout.encoding
UTF-8
#!/usr/bin/env python3.2
# -*- coding: UTF-8 -*-

from __future__ import print_function
from __future__ import unicode_literals

import re
import sys
import os

if not (("PYTHONIOENCODING" in os.environ)
            and
        re.search("^utf-?8$", os.environ["PYTHONIOENCODING"], re.I)):
    sys.stderr.write(sys.argv[0] + ": Please set your PYTHONIOENCODING envariable to utf8\n")
    sys.exit(1)

import unicodedata
if unicodedata.unidata_version < "6.0.0":
    print("WARNING: Your old UCD is out of date, expected at least 6.0.0 but got", 
           unicodedata.unidata_version)

wide_enough = (sys.maxunicode >= 0x10FFFF)
if not wide_enough:
    print("WARNING: Narrow build detected, your Python lacks full Unicode support!!")
然后对Unicode字符串进行相应的编码:

>>> u"Ä"
u'\xc4'
>>> sys.stdout.write(u"Ä".encode(enc, 'replace'))

当一个字符在终端的编码中不可表示时,可以避免UnicodeEncodeErrors。这将被一个问号替换。

强烈建议您不要使用“?”作为替换字符。只需将输出编码设置为UTF-8就可以了

for s in ("stdin","stdout","stderr"): 
   setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(), encoding="utf8"))
或者,将
PYTHONIOENCODING
envariable设置为utf8,以便python停止猜测输出编码

这两种方法都比手工编码好得多,因为手工编码很愚蠢

如果您拒绝升级到Python3,我也建议您

from __future__ import unicode_literals
为了赶走那些愚蠢的东西

最近我启动了所有Python程序,如下所示:

>>> import sys
>>> sys.stdout.encoding
UTF-8
#!/usr/bin/env python3.2
# -*- coding: UTF-8 -*-

from __future__ import print_function
from __future__ import unicode_literals

import re
import sys
import os

if not (("PYTHONIOENCODING" in os.environ)
            and
        re.search("^utf-?8$", os.environ["PYTHONIOENCODING"], re.I)):
    sys.stderr.write(sys.argv[0] + ": Please set your PYTHONIOENCODING envariable to utf8\n")
    sys.exit(1)

import unicodedata
if unicodedata.unidata_version < "6.0.0":
    print("WARNING: Your old UCD is out of date, expected at least 6.0.0 but got", 
           unicodedata.unidata_version)

wide_enough = (sys.maxunicode >= 0x10FFFF)
if not wide_enough:
    print("WARNING: Narrow build detected, your Python lacks full Unicode support!!")
#/usr/bin/env蟒蛇3.2
#-*-编码:UTF-8-*-
来自未来导入打印功能
从未来导入unicode文字
进口稀土
导入系统
导入操作系统
如果不是((“pythonionecoding”在os.environ中)
和
重新搜索(“^utf-?8$”,os.environ[“PythonionEncoding”],re.I)):
sys.stderr.write(sys.argv[0]+“:请将PythonionEncoding环境变量设置为utf8\n”)
系统出口(1)
导入Unicode数据
如果unicodedata.unidata_版本<“6.0.0”:
打印(“警告:您的旧UCD已过期,预计至少为6.0.0,但已过期”,
unicodedata.unidata_版本)
足够宽=(sys.maxunicode>=0x10FFFF)
如果不够宽:
打印(“警告:检测到窄版本,您的Python缺少完全的Unicode支持!!”)

强烈建议您不要使用“?”作为替换字符。只需将输出编码设置为UTF-8就可以了

for s in ("stdin","stdout","stderr"): 
   setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(), encoding="utf8"))
或者,将
PYTHONIOENCODING
envariable设置为utf8,以便python停止猜测输出编码

这两种方法都比手工编码好得多,因为手工编码很愚蠢

如果您拒绝升级到Python3,我也建议您

from __future__ import unicode_literals
为了赶走那些愚蠢的东西

最近我启动了所有Python程序,如下所示:

>>> import sys
>>> sys.stdout.encoding
UTF-8
#!/usr/bin/env python3.2
# -*- coding: UTF-8 -*-

from __future__ import print_function
from __future__ import unicode_literals

import re
import sys
import os

if not (("PYTHONIOENCODING" in os.environ)
            and
        re.search("^utf-?8$", os.environ["PYTHONIOENCODING"], re.I)):
    sys.stderr.write(sys.argv[0] + ": Please set your PYTHONIOENCODING envariable to utf8\n")
    sys.exit(1)

import unicodedata
if unicodedata.unidata_version < "6.0.0":
    print("WARNING: Your old UCD is out of date, expected at least 6.0.0 but got", 
           unicodedata.unidata_version)

wide_enough = (sys.maxunicode >= 0x10FFFF)
if not wide_enough:
    print("WARNING: Narrow build detected, your Python lacks full Unicode support!!")
#/usr/bin/env蟒蛇3.2
#-*-编码:UTF-8-*-
来自未来导入打印功能
从未来导入unicode文字
进口稀土
导入系统
导入操作系统
如果不是((“pythonionecoding”在os.environ中)
和
重新搜索(“^utf-?8$”,os.environ[“PythonionEncoding”],re.I)):
sys.stderr.write(sys.argv[0]+“:请将PythonionEncoding环境变量设置为utf8\n”)
系统出口(1)
导入Unicode数据
如果unicodedata.unidata_版本<“6.0.0”:
打印(“警告:您的旧UCD已过期,预计至少为6.0.0,但已过期”,
unicodedata.unidata_版本)
足够宽=(sys.maxunicode>=0x10FFFF)
如果不够宽:
打印(“警告:检测到窄版本,您的Python缺少完全的Unicode支持!!”)

文本的输入编码(此处为utf-8)无关紧要。您应该尽快将utf8 bytestring转换为Unicode,然后打印文本:

print(unicode_text)
  • 打印前不要将文本编码为utf8
  • 不要修改
    sys.stdout
    以使用utf8对文本进行编码
输出编码由运行脚本的环境根据区域设置(
LANG
LC\u CTYPE
LC\u ALL
)或
PYTHONIOENCODING
envar)指定。不要无条件地输出utf8

例如,如果未设置区域设置;您可以明确指定它:

$ LANG=en_US.utf8 python your_script.py
确保您的终端能够显示相应的Unicode字符(已安装字体、相应的区域设置(
locale-a


换句话说,要修复输出,就要修复环境,例如。

文本的输入编码(此处为utf-8)并不重要。您应该尽快将utf8 bytestring转换为Unicode,然后打印文本:

print(unicode_text)
  • 打印前不要将文本编码为utf8
  • 不要修改
    sys.stdout
    以使用utf8对文本进行编码
输出编码由运行脚本的环境根据区域设置(
LANG
LC\u CTYPE
LC\u ALL
)或
PYTHONIOENCODING
envar)指定。不要无条件地输出utf8

例如,如果未设置区域设置;您可以明确指定它:

$ LANG=en_US.utf8 python your_script.py
确保您的终端能够显示相应的Unicode字符(已安装字体、相应的区域设置(
locale-a


换句话说,要修复输出,请修复环境,例如。

您确定您的区域设置与终端的实际功能匹配吗?永远不要依赖终端设置。将所有的编码设置为UTF-8并消除所有的海森堡。你确定你的区域设置与终端的实际功能相匹配吗?永远不要依赖终端设置。将所有的编码设置为UTF-8,并消除所有的海森堡。谢谢。在我使用的编辑中:
import-re-import-sys-import-os-if-not((“os.environ中的pythonionecoding”)和re.search(^utf-?8$,os.environ[“pythonionecoding”],re.I)):sys.stderr.write(sys.argv[0]+”:请