今天爱分享给大家带来Python logging.FileHandler 文件中文字符乱码【解决办法】,希望能够帮助到大家。
解决方法很简单,在定义Filehandler时指定encoding就好了
import logging file_hanlder = logging.FileHandler(filename='example.log', encoding='utf-8')
因为看python的logging.FileHandler()类的代码如下,就知道Filehanlder也支持指定编码格式的了,原代码如下:
# logging\__init__.py
class FileHandler(StreamHandler):
"""
A handler class which writes formatted logging records to disk files.
"""
def __init__(self, filename, mode='a', encoding=None, delay=False):
"""
Open the specified file and use it as the stream for logging.
"""
# Issue #27493: add support for Path objects to be passed in
filename = os.fspath(filename)
...
原文链接:https://itblood.website/2045.html,转载请注明出处。

![遗产v0.13.0.0 AI版[PC+安卓/2.02G/更新]D’Legacy [v0.13.0.0] [真人SLG/汉化/沙盒]](/wp-content/uploads/replace/2025/10/31/9a71cd440a0ca6560d03df89bb836e2e.webp)