首页 > 解决方案 > spyder 中的 UnicodeEncodeError Python 与活动进度

问题描述

我在 W10 中使用来自 anaconda 的 Spyder 4.1.1 和 Python 3.7.6。我正在为我的一个项目使用模块 alive-progress。只需使用以下代码的基本行:

from alive_progress import alive_bar
import time


for x in 3000, 4000, 2000, 0:
    with alive_bar(x) as bar:
        for i in range(3000):
            time.sleep(0.002)
            bar(0)

我收到下一个错误:

Traceback (most recent call last):

  File "\\My_path\alive_bar.py", line 10, in <module>
    bar(0)

  File "C:\Users\delprf2\Anaconda3\lib\contextlib.py", line 119, in __exit__
    next(self.gen)

  File "C:\Users\delprf2\Anaconda3\lib\site-packages\alive_progress\progress.py", line 275, in alive_bar
    alive_repr()

  File "C:\Users\delprf2\Anaconda3\lib\site-packages\alive_progress\progress.py", line 118, in alive_repr
    sys.__stdout__.write(line + (spin and '\r' or '\n'))

  File "C:\Users\delprf2\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-40: character maps to <undefined>

有没有人如何解决它?提前致谢

标签: python-3.xprogress-barspyderencode

解决方案


我是生活进步的作者!

您的系统 python 或终端似乎不接受 unicode 字符。我不知道anaconda,但为什么它使用cp1252?它应该默认接受 utf8。

无论如何,请尝试使用不使用任何花哨字符的主题:

from alive_progress import alive_bar
import time


for x in 3000, 4000, 2000, 0:
    with alive_bar(x, theme='ascii') as bar:
        for i in range(3000):
            time.sleep(0.002)
            bar()

请在以下位置找到更多详细信息:https ://github.com/rsalmei/alive-progress 如果您有任何其他疑问,您可以随时在其中打开问题,我会帮助您。


推荐阅读