Python没有从emojis捕获UnicodeError

2024-06-26 02:52:05 发布

您现在位置:Python中文网/ 问答频道 /正文

以前,使用非bmp贴图翻译名称可以防止名称中的表情符号破坏脚本,但突然它不起作用,我不知道为什么

我尝试添加一个try/except块来查看问题所在,但except根本无法捕获异常。我使用了调试器,代码跳转到except块(!?)后面的行

下面是有问题的代码:

![enter image description here

线路

logger.info(f"Liking tweet from user {name}")

引发以下异常:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\JGC\anaconda3\lib\logging\__init__.py", line 1028, in emit
    stream.write(msg + self.terminator)
  File "C:\Users\JGC\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 character '\ufffd' in position 40: character maps to <undefined>
Call stack:
  File "C:\Users\JGC\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\JGC\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>        
    cli.main()
  File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\JGC\.vscode\extensions\ms-python.python-2020.6.91350\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "C:\Users\JGC\anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\JGC\anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\JGC\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\JGC\Desktop\Python\Templates de bots\Twitter Template\posting-bot\machado.py", line 169, in <module>
    stream.filter(track=HASHTAGS, languages=["pt"])
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 474, in filter
    self._start(is_async)
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 389, in _start
    self._run()
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 289, in _run
    self._read_loop(resp)
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 351, in _read_loop
    self._data(next_status_obj)
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 323, in _data
    if self.listener.on_data(data) is False:
  File "C:\Users\JGC\anaconda3\lib\site-packages\tweepy\streaming.py", line 54, in on_data
    if self.on_status(status) is False:
  File "c:\Users\JGC\Desktop\Python\Templates de bots\Twitter Template\posting-bot\machado.py", line 59, in on_status
    logger.info(f"Liking tweet from user {name}")
Message: 'Liking tweet from user maya | �; aridante'
Arguments: ()

Tags: runnameinpyselfmainlibpackages