UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符:序数不在范围内(128) - python

我正在使用使用剪刀字符(9986-✂)的Python脚本,正在尝试将代码移植到Mac,但是遇到了此错误。

从IDLE(Python 3.2.5-OS X 10.4.11 iBook G4 PPC)运行时,剪刀字符显示正常,并且代码在Ubuntu 13.10上完全正常,但是当我尝试在终端上运行时,出现此错误/追溯:

Traceback (most recent call last):
  File "snippets-convert.py", line 352, in <module>
    main()
  File "snippets-convert.py", line 41, in main
    menu()
  File "snippets-convert.py", line 47, in menu
    print ("|\t ",snipper.decode(),"PySnipt'd",snipper.decode(),"\t|")
UnicodeEncodeError: 'ascii' codec can't encode character '\u2702' in position 0: ordinal not in range(128)

和给我这个问题的代码:
print ("|\t ",chr(9986),"PySnipt'd",chr(9986),"\t|")
这不是信号表明终端没有显示该字符的能力吗?我知道这是一个旧系统,但是它是目前我唯一需要使用的系统。操作系统的年龄是否会干扰程序?

我已阅读以下问题:

  • UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128)-不同字符
  • "UnicodeEncodeError: 'ascii' codec can't encode character"-使用2.6,所以不知道它是否适用
  • UnicodeEncodeError: 'ascii' codec can't encode character?-似乎可以解决我的问题.encode('UTF-8'),我没有收到错误。但是,它显示的是字符代码,而不是我想要的字符,而.decode()却给了我同样的错误。不知道我是否做对了。
  • UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)-不确定是否适用,他正在使用GUI,获取输入以及所有希腊文内容。
  • 是什么导致此错误?是系统/操作系统的时代,Python的版本还是某些编程错误?

    编辑:
    此错误稍后会因重复的问题而出现(只是以为我会将其添加到同一程序中并且是同一错误):

    Traceback (most recent call last):
      File "snippets-convert.py", line 353, in <module>
        main()
      File "snippets-convert.py", line 41, in main
        menu()
      File "snippets-convert.py", line 75, in menu
        main()
      File "snippets-convert.py", line 41, in main
        menu()
      File "snippets-convert.py", line 62, in menu
        search()
      File "snippets-convert.py", line 229, in search
        print_results(search_returned)      # Print the results for the user
      File "snippets-convert.py", line 287, in print_results
        getPath(toRead)                                             # Get the path for the snippet
      File "snippets-convert.py", line 324, in getPath
        snipXMLParse(path)
      File "snippets-convert.py", line 344, in snipXMLParse
        print (chr(164),child.text)
    UnicodeEncodeError: 'ascii' codec can't encode character '\xa4' in position 0: ordinal not in range(128)
    

    编辑:

    我进入了终端字符设置,它实际上支持该字符(如您在此屏幕快照中所见:

    当我将其插入终端时,它会打印出:\342\234\202,当我按Enter时,我会得到:-bash: ✂: command not found
    编辑以@ J.F格式运行命令。塞巴斯蒂安问:
    python3 test-io-encoding.py:

    PYTHONIOENCODING:       None
    locale(False):  US-ASCII
    device(stdout): US-ASCII
    stdout.encoding:        US-ASCII
    device(stderr): US-ASCII
    stderr.encoding:        US-ASCII
    device(stdin):  US-ASCII
    stdin.encoding: US-ASCII
    locale(False):  US-ASCII
    locale(True):   US-ASCII
    

    python3 -S test-io-encoding.py:

    PYTHONIOENCODING:       None
    locale(False):  US-ASCII
    device(stdout): US-ASCII
    stdout.encoding:        US-ASCII
    device(stderr): US-ASCII
    stderr.encoding:        US-ASCII
    device(stdin):  US-ASCII
    stdin.encoding: US-ASCII
    locale(False):  US-ASCII
    locale(True):   US-ASCII
    

    编辑尝试了@PauloBu提供的“黑客”解决方案:

    如您所见,这导致一个(是!)剪刀,但是我现在遇到了一个新错误。追溯/错误:

    +-=============================-+
    ✂Traceback (most recent call last):
      File "snippets-convert.py", line 357, in <module>
        main()
      File "snippets-convert.py", line 44, in main
        menu()
      File "snippets-convert.py", line 52, in menu
        print("|\t "+sys.stdout.buffer.write(chr(9986).encode('UTF-8'))+" PySnipt'd "+ sys.stdout.buffer.write(chr(9986).encode('UTF-8'))+" \t|")
    TypeError: Can't convert 'int' object to str implicitly
    

    编辑添加了@PauloBu的修复结果:

    +-=============================-+
    |
    ✂ PySnipt'd 
    ✂       |
    +-=============================-+
    

    编辑:

    他的解决方法是:

    +-=============================-+
    ✂✂|       PySnipt'd     |
    +-=============================-+
    

    参考方案

    当Python打印并输出时,它将自动将其编码为目标介质。如果是文件,则默认使用UTF-8,每个人都会高兴,但是如果是终端,Python将确定终端使用的编码,并尝试使用该编码。

    这意味着如果您的终端使用ascii作为编码,Python会尝试将scissor char编码为ascii。当然,ascii不支持它,因此会出现Unicode解码错误。

    这就是为什么始终必须显式编码输出的原因。显式胜于隐式还记得吗?要修复您的代码,您可以执行以下操作:

    import sys
    sys.stdout.buffer.write(chr(9986).encode('utf8'))
    

    这似乎有点骇人听闻。您还可以在执行脚本之前设置PYTHONIOENCODING = utf-8。我对这两种解决方案都不满意。可能您的控制台不支持utf-8,并且您看到乱码。但是您的程序将表现正确。

    如果您肯定需要在控制台上显示正确的输出,我强烈建议您将控制台设置为使用另一种编码,该编码支持scissor字符。 (也许是utf-8)。在Linux上,可以这样做:export lang=UTF_8。在Windows上,您可以使用chcp更改控制台的代码页。只需弄清楚如何在您的菜单中设置utf8,恕我直言,这将是最佳解决方案。

    您不能混合使用printsys.stdout.write,因为它们基本相同。关于您的代码,黑客方式将如下所示:

    sys.stdout.buffer.write(("|\t "+ chr(9986) +" PySnipt'd " + chr(9986)+" \t|").encode('utf8'))
    

    我建议您阅读文档,以了解print函数和sys.stdout背后的情况:http://docs.python.org/3/library/sys.html#sys.stdin

    希望这可以帮助!

    UnicodeDecodeError:'ascii'编解码器无法解码位置8的字节0xc3:序数不在范围内(128) - python

    我在此行收到此错误:logger.debug(u'__call__ with full_name={}, email={}'.format(full_name, email)) 为什么?name变量的内容为Gonçalves。 参考方案 这应该可以解决您的问题:full_name, email = [unicode(x, 'ut…

    单行的'if'/'for'语句是否使用Python样式好? - python

    我经常在这里看到某人的代码,看起来像是“单线”,这是一条单行语句,以传统的“if”语句或“for”循环的标准方式执行。我在Google周围搜索,无法真正找到可以执行的搜索类型?任何人都可以提出建议并最好举一些例子吗?例如,我可以一行执行此操作吗?example = "example" if "exam" in exam…

    为什么使用'=='或'is'比较字符串有时会产生不同的结果? - python

    我有一个Python程序,其中将两个变量设置为'public'值。在条件表达式中,我有比较var1 is var2失败,但如果将其更改为var1 == var2,它将返回True。现在,如果我打开Python解释器并进行相同的“是”比较,则此操作成功。>>> s1 = 'public' >>…

    在返回'Response'(Python)中传递多个参数 - python

    我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

    python JSON对象必须是str,bytes或bytearray,而不是'dict - python

    在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…