简体   繁体   English

UnicodeEncodeError:即使将记录器格式化为使用 utf-8 编码,“charmap”编解码器也无法对字符进行编码

[英]UnicodeEncodeError: 'charmap' codec can't encode character even after formatting logger to use utf-8 encoding

I am getting several of these errors from my logger in python.我从我的 Python 记录器中收到了其中的几个错误。

UnicodeEncodeError: 'charmap' codec can't encode character X in position Y : character maps to undefined UnicodeEncodeError: 'charmap' 编解码器无法对位置Y 中的字符X进行编码:字符映射到未定义

Looking around on stackoverflow, I see that many people have been able to resolve the issue by telling their logger to use 'utf-8' encoding however for me the error still remains.环顾stackoverflow,我看到很多人已经能够通过告诉他们的记录器使用“utf-8”编码来解决这个问题,但是对我来说错误仍然存​​在。

This is the function that I am using to create my loggers:这是我用来创建记录器的函数:

def makeLogger(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')

    file_handler = logging.FileHandler(r'C:\Users\lguarro\Documents\Work\SearchEngine_Pure\Log\custom.log', 'a', 'utf-8')
    file_handler.setFormatter(formatter)

    logger.addHandler(file_handler)
    return logger

As you can see I have explicitly told the FileHandler to use utf-8.如您所见,我已明确告诉 FileHandler 使用 utf-8。

Here is an example of how I call my logger from which many errors originate:这是我如何调用我的记录器的示例,其中许多错误源自:

self.logger.info("Starting url scrape for company, " + rec["Company"] + " using user agent: " + user_agent)

In particular the error typically comes from the characters inside rec["Company"] as there are a lot of weird company names in my database like lantm√§nnen unibake.特别是错误通常来自 rec["Company"] 中的字符,因为我的数据库中有很多奇怪的公司名称,如 lantm√§nnen unibake。

So what exactly am I missing?那么我到底错过了什么?

My issue was that I was using Scrapy and I was not configuring the root logger correctly and was only setting up the logger for each of my individual spiders (which behaves correctly)我的问题是我使用的是 Scrapy 并且我没有正确配置根记录器,只是为我的每个蜘蛛设置记录器(行为正确)

In essence, I needed to add this to my execution script:本质上,我需要将它添加到我的执行脚本中:

root_logger = logging.getLogger()
root_logger.setLevel(logging.WARNING)
handler = logging.FileHandler(r'C:\Users\lguarro\Documents\Work\SearchEngine_Pure\Log\scrapy.log', 'w', 'utf-8')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(message)s'))
root_logger.addHandler(handler)

For whatever reason, when I was trying to do the same thing using logging.basicConfig, it told me that "encoding" is not a valid attribute so I opted for this method instead.不管什么原因,当我尝试使用 logging.basicConfig 做同样的事情时,它告诉我“编码”不是一个有效的属性,所以我选择了这个方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character &#39;\ě&#39; in position 57: character maps to<undefined> (但无法使用 UTF-8) - UnicodeEncodeError: 'charmap' codec can't encode character '\u011b' in position 57: character maps to <undefined> (but unable to use UTF-8) UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode character 字符编码错误:UnicodeEncodeError:&#39;charmap&#39;编解码器无法对位置Y中的字符X进行编码:字符映射到<undefined> - Character encoding error: UnicodeEncodeError: 'charmap' codec can't encode character X in position Y: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; 编解码器无法编码字符:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character : character maps to <undefined> UnicodeEncodeError:&#39;charmap&#39;编解码器无法对字符字符映射进行编码<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character character maps to <undefined> Pandas UnicodeEncodeError: &#39;charmap&#39; 编解码器无法编码字符 - Pandas UnicodeEncodeError: 'charmap' codec can't encode character UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符...问题 - UnicodeEncodeError: 'charmap' codec can't encode character… problems UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符&#39;\\ u2264&#39; - UnicodeEncodeError: 'charmap' codec can't encode character '\u2264' MongoDB中的Python错误“ UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符” - Python error “UnicodeEncodeError: 'charmap' codec can't encode character” in MongoDB UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM