在python 3中记录imodule时出现UTF-8编码问题

在python 3中记录imodule时出现UTF-8编码问题,python,python-3.x,logging,utf-8,Python,Python 3.x,Logging,Utf 8,在我的python(3.7.4)应用程序中,我试图并行分析几个xml文件(用utf-8编码)(几个线程),并记录一些关于日志中的行的消息 我的代码: import logging .... logging.basicConfig(filename='app.log', filemode='w',format='[%(asctime)s] [%(process)d][%(threadName)s - %(thread)d]- %(message)s', level=logging.INFO,enc

在我的python(3.7.4)应用程序中,我试图并行分析几个xml文件(用utf-8编码)(几个线程),并记录一些关于日志中的行的消息

我的代码:

import logging
....
logging.basicConfig(filename='app.log', filemode='w',format='[%(asctime)s] [%(process)d][%(threadName)s - %(thread)d]- %(message)s', level=logging.INFO,encoding='utf-8')
xmldoc = minidom.parse(xml_file)
logging.info("starting to parse file : {}".format(xml_file))
itemsList = xmldoc.getElementsByTagName("Item")
for item in itemsList:
    currentItemName = item.getElementsByTagName("itemName")[0].firstChild.data
    logging.info("parsing item :{}".format(currentItemName))
    ...
我在日志记录行中遇到的错误:

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

如果有人能帮我解决这个问题,我会很高兴的。

似乎一旦配置了根记录器,就无法编辑其设置。我在另一个文件中配置了记录器的基本配置,这就是为什么当前文件中的所有更改(如启用处理程序的编码)都没有生效。

encoding=“utf-8”
应该可以工作。@snakecharmerb可能重复-我尝试过,没有帮助(还更新了我的主要注释)似乎一旦配置根记录器,就无法编辑其设置。我在另一个文件中配置了记录器的基本配置,这就是为什么当前文件中的所有更改(如启用处理程序的编码)都没有生效。
<?xml version="1.0" encoding="utf-8"?>
--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 1028, in emit
    stream.write(msg + self.terminator)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python37-32\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 82-87: character maps to <undefined>
Call stack:
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 890, in _bootstrap
    self._bootstrap_ifnner()
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\myuser\PycharmProjects\myproj\pars\xmlparsers.py", line 88, in parseXML
    logging.info("parsing item :{}".format(currentItem))
Message: 'parsing item לד500גר'
logging.basicConfig(
    handlers=[logging.FileHandler('main.log', 'w', 'utf-8')],
    format='[%(asctime)s] [%(process)d][%(threadName)s - %(thread)d]- %(message)s',
    level=logging.INFO,
    encoding='UTF-8')