python打印中文报错_Python 3.6 logging模块打印中文日志报错
时间: 2024-02-27 19:58:29 浏览: 160
在 Python 3.x 中,logging 模块默认使用 ASCII 编码,因此在打印中文时会报错。解决方法是在代码中加入以下语句:
```python
import logging
import sys
# 设置日志输出格式
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
stream=sys.stdout)
# 设置中文编码
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
handler.setLevel(logging.DEBUG)
handler.addFilter(lambda record: not record.getMessage().startswith('[DEBUG]'))
logging.getLogger().addHandler(handler)
```
其中,`handler.setFormatter` 方法用于设置日志输出格式,`handler.setLevel` 方法用于设置日志输出级别,`handler.addFilter` 方法用于过滤掉一些不需要输出的日志。最后,将 handler 加入到 logger 中即可。
相关问题
Traceback (most recent call last): File "D:\Python\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "D:\Python\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\__main__.py", line 29, in <module> from pip._internal.cli.main import main as _main File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\cli\main.py", line 9, in <module> from pip._internal.cli.autocompletion import autocomplete File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\cli\autocompletion.py", line 10, in <module> from pip._internal.cli.main_parser import create_main_parser File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\cli\main_parser.py", line 9, in <module> from pip._internal.build_env import get_runnable_pip File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\build_env.py", line 20, in <module> from pip._internal.cli.spinners import open_spinner File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\cli\spinners.py", line 9, in <module> from pip._internal.utils.logging import get_indentation File "D:\Python\lib\site-packages\pip-22.3.1-py3.6.egg\pip\_internal\utils\logging.py", line 8, in <module> from dataclasses import dataclass ModuleNotFoundError: No module named 'dataclasses'
这个错误提示是由于你的 Python 版本过低,没有内置 dataclasses 模块所导致的。dataclasses 是在 Python 3.7 中引入的,如果你使用的是 Python 3.6 或更低的版本,那么就无法使用它。
你可以通过升级 Python 版本来解决这个问题。你可以去 Python 官网下载最新版本的 Python,然后安装并配置好环境变量。
如果你不想升级 Python 版本,也可以使用其他第三方库来替代 dataclasses。例如,你可以使用 attrs 库来实现类似的功能。你可以使用以下命令来安装 attrs 库:
```
pip install attrs
```
然后,在你的 Python 代码中,你可以使用 attrs 来定义类,例如:
```
import attr
@attr.s
class Person:
name = attr.ib()
age = attr.ib()
```
这样就可以实现类似于 dataclasses 的功能了。希望这个解决方案能够帮助你解决问题。
ERROR: Exception: Traceback (most recent call last): File "/usr/lib/python3.6/shutil.py", line 550, in move os.rename(src, real_dst) PermissionError: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages/numpy' -> '/tmp/pip-uninstall-i14esy8v' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 164, in exc_logging_wrapper status = run_func(*args) File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/commands/uninstall.py", line 99, in run verbose=self.verbosity > 0, File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/req/req_install.py", line 671, in uninstall uninstalled_pathset.remove(auto_confirm, verbose) File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/req/req_uninstall.py", line 384, in remove moved.stash(path) File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/req/req_uninstall.py", line 282, in stash renames(path, new_path) File "/home/daxigua/.local/lib/python3.6/site-packages/pip/_internal/utils/misc.py", line 307, in renames shutil.move(old, new) File "/usr/lib/python3.6/shutil.py", line 562, in move rmtree(src) File "/usr/lib/python3.6/shutil.py", line 486, in rmtree _rmtree_safe_fd(fd, path, onerror) File "/usr/lib/python3.6/shutil.py", line 424, in _rmtree_safe_fd _rmtree_safe_fd(dirfd, fullname, onerror) File "/usr/lib/python3.6/shutil.py", line 424, in _rmtree_safe_fd _rmtree_safe_fd(dirfd, fullname, onerror) File "/usr/lib/python3.6/shutil.py", line 444, in _rmtree_safe_fd onerror(os.unlink, fullname, sys.exc_info()) File "/usr/lib/python3.6/shutil.py", line 442, in _rmtree_safe_fd os.unlink(name, dir_fd=topfd) PermissionError: [Errno 13] Permission denied: 'test_linalg.py'
这个错误是在卸载NumPy时出现的,可能是因为您没有足够的权限来卸载NumPy。您可以尝试使用sudo命令来解决这个问题。请按照以下步骤操作:
1. 打开终端并以管理员权限运行以下命令:
```
sudo pip uninstall numpy
```
2. 然后输入您的密码并按回车键。
3. 如果您仍然遇到权限问题,请尝试在命令前加上sudo命令并重新运行命令:
```
sudo pip uninstall numpy
```
希望这些步骤可以解决您的问题。如果问题仍然存在,请提供更多的错误信息,以便我们更好地帮助您。
阅读全文