今天爱分享给大家带来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)
...
原文链接:http://blog.itblood.com/2045.html,转载请注明出处。

![迷失的季节 Seasons of Loss ver1.031 官方中文版 [2.90G] [亚洲风SLG/绿母/动态/更新]](/wp-content/uploads/replace/2025/07/19/c96fbbb2069395643f9caba428084594.webp)