最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】Python3中,使用logging.debug结果出错:UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in position 74: illegal multibyte sequence

Python3 crifan 6682浏览 0评论

【问题】

Python 3中,使用logging.debug结果出错:

type(foundAllItems)= <class 'bs4.element.ResultSet'>
Traceback (most recent call last):
  File "E:\dev_install_root\Python32\lib\logging\__init__.py", line 939, in emit
    stream.write(msg)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 179: illegal multibyte sequence

对应的代码为:

    foundAllItems = soup.findAll(attrs={"class":"countryInfo"});
    print("type(foundAllItems)=", type(foundAllItems));
    logging.debug("str(foundAllItems)=%s", str(foundAllItems));

if __name__=="__main__":
    logging.basicConfig(
                    level    = logging.DEBUG,
                    format   = 'LINE %(lineno)-4d  %(levelname)-8s %(message)s',
                    datefmt  = '%m-%d %H:%M',
                    filename = scriptSelfName + ".log",
                    filemode = 'w');
    # define a Handler which writes INFO messages or higher to the sys.stderr
    console = logging.StreamHandler();
    console.setLevel(logging.INFO);
    # set a format which is simpler for console use
    formatter = logging.Formatter('LINE %(lineno)-4d : %(levelname)-8s %(message)s');
    # tell the handler to use this format
    console.setFormatter(formatter);
    logging.getLogger('').addHandler(console);

 

【解决过程】

1.对于UnicodeEncodeError,之前使用Python 2的时候,已经遇到过N多次了,而且都已解决了。

所以,对于Python 2中的UnicodeEncodeError,现在来说,都不是问题。

但是此处是Python 3,却也遇到了UnicodeEncodeError错误。

2.对于上述

logging.debug("str(foundAllItems)=%s", str(foundAllItems));

其实之前用的是:

logging.debug("foundAllItems=%s", foundAllItems);

结果也还是同样错误的。

3.想要尝试去logging中找encode,encoding,charset等方面的设置,也都没有找到。

4.从错误的结果来看,很明显是,对于Python3 中的字符串foundAllItems,输入到console中,结果由于当前console是windows下的cmd,默认编码是GBK,

而foundAllItems字符串中,包含了GBK没法解码的\xa0,所以出错了。

但是很奇怪的是:

(1)对于此处Python3 中的字符串foundAllItems,其应该是相当于Python 2中的unicode,所以unicode类型字符串通过logging系统去输出的话,之前在Python 2中,从来没出错。

(倒是在Python 2中,使用logging输入,但是输入的字符串是str类型,是某种编码,比如utf-8,但是输入到GBK编码的cmd等情况时,才会出现这类UnicodeEncodeError的错误的)

(2)对于logging系统来说,最开始的配置是level为logging.debug,所以此处的通过

logging.debug("str(foundAllItems)=%s", str(foundAllItems));

所输出的内容,应该是直接输出到之前所设定的.log文件中去,而不应该输入到此处的console,即windows的cmd中去才对。

 

所以,以上两点,始终是很奇怪,没有找到合理的解释。

5.网上找了半天,唯一和我此处问题有点关系的算是:

UnicodeEncodeError: ‘gbk’ codec can’t encode character: illegal multibyte sequence

但是也还是对我此问题的解决,没啥帮助。

因为其说的是对于str,即unicode去编码,然后加上ignore之类参数,这些我都是知道的,但是此处是无法实施,因为是通过logging系统输出的,

而如果对于str,即unicode去encode的话,则logging系统又会抱怨是bytes,而不是str了。

 

【总结】

此问题,前面所说的两个疑问,仍未解决。问题本身也未解决。

看来是,等解决了上述两个疑问,此问题也就迎刃而解了。

希望有高手给出解决办法。

转载请注明:在路上 » 【未解决】Python3中,使用logging.debug结果出错:UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in position 74: illegal multibyte sequence

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (3)

  1. basicconfig 里面有encoding这一项,可以尝试一下
    fan7年前 (2017-10-06)回复
  2. 我遇到了这个问题,当我在CMD命令行中执行自己写的解析html文本时,出现下面的错误信息: Traceback (most recent call last): File "my_html.py", line 43, in ''') File "D:\Program Files\Python\Python35\lib\html\parser.py", line 111, in feed self.goahead(0) File "D:\Program Files\Python\Python35\lib\html\parser.py", line 163, in goahead self.handle_data(unescape(rawdata[i:j])) File "my_html.py", line 22, in handle_data print(data) UnicodeEncodeError: 'cp932' codec can't encode character '\xa0' in position 5: illegal multibyte sequence 我使用chcp命令CMD的code page,命令如下: E:\git\mypython> chcp 65001 然后再执行前面的命令,可以正常执行。 总结:待输出的文本在内存中是Unicode编码的,当要输出到文件或者终端的时,会以文件或终端的编码格式进行编码。这时如果内存中的文本对应的Unicode编码不能编码成文件或者终端对应的编码,就会出现编码错误。
    独狼刺8年前 (2016-09-30)回复
  3. 我解决的方案的是:用FileHandler,这个可以指定encoding,结果是正确的。见文档:https://docs.python.org/3/library/logging.handlers.html
    猛禽8年前 (2016-03-10)回复
86 queries in 0.205 seconds, using 22.18MB memory