python 写入日志的问题 UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence
最近,使用python的logging模块,因为这个写入日志写完后就没有管它。在存储日志信息的时候,一直提示:
UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence
还是以为是文件编码有问题,困扰了很久,其实是在写入日志时候提示的编码错误。
所以,需要对日志函数做一定的修改,编码改为utf-8
def get_logger(log_file):
"""
进行日志操作,有专门的日志模块:logging
:param log_file:
:return:
"""
logger = logging.getLogger(log_file)
# 设置日志等级
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(log_file, encoding='utf8')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
return logger
python 写入日志的问题 UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence的更多相关文章
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence
使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 30633: illegal multibyte sequence
import urllib.request def load_baidu(): url = "https://www.baidu.com/" header = {"Use ...
- 解决python3.6的UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 28613: illegal multibyte sequence
这是python3.6的print()函数自身有限制,不能完全打印所有的unicode字符. 主要的是windows下python的默认编码不是'utf-8',改一下python的默认编码成'utf- ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 26269: illegal multibyte sequence
解决方法参见下面的链接: http://blog.csdn.net/jim7424994/article/details/22675759
- python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 30: illegal multibyte sequence
昨天用用python3写个日志文件,结果报错UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 30: ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 1987: illegal multibyte sequence
在爬取 url = "http://stats.meizhou.gov.cn/show/index/1543/1689" 时出现了问题: UnicodeEncodeError: ' ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\u25aa' in position 15: illegal multibyte sequence
UnicodeEncodeError: 'gbk' codec can't encode character '\u25aa' in position 15: illegal multibyte se ...
- day1 UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 2490: illegal multibyte sequence 错误提示
get方式得到网页的信息 #coding=utf-8 #pip install requests #直接get到网页的信息 import requests from bs4 import Beauti ...
- python3 UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in position 230: illegal multibyte sequence
最近在保存微博数据到(csv文件)时报错: UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in positio ...
随机推荐
- Windows Server 2008 + SQL Server 2005集群
一. 基础环境 1. 服务器规划 2. 网络拓扑 二. 相关说明 1.为了节约服务器资源,AD服务器可以和iSCSI设备服务器同为一台服务器.由于iSCSI软件需要,目前微软只开发了基于Windows ...
- Lab6: Paxos
Introduction In labs 6 and 7, you will replicate the lock service using the replicated state machine ...
- Python中的类(上)
在Python中,可以通过class关键字定义自己的类,然后通过自定义的类对象类创建实例对象. 例如,下面创建了一个Student的类,并且实现了这个类的初始化函数"__init__&quo ...
- flexbox父盒子flex-direction属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用 vux 框架
1)vux官网:https://vux.li/#/ 2)通过 vue-cli 工具使用 vux 1.如果没有安装 nodejs,请先前往 nodejs 官网下载并安装 nodejs,传送门:https ...
- Studio更新
其实最主要的是下面三个步骤: 1.更新As工程为3.0 2.必须升级gradle到4.0以上 3.buildToolsVersion升级到26.0.0 4.在gradle.properties中配置版 ...
- make: Warning: File `Makefile' has modification time 17 s in the future
linux下,make makefile文件的时候报警告: make: Warning: File `Makefile' has modification time 17 s in the futur ...
- spring配置文件中xsd引用问题
XML的一些概念 首先来看下xml的一些概念: xml的schema里有namespace,可以给它起个别名.比如常见的spring的namespace: xmlns:mvc="http ...
- Shell find命令详解
查找文件find ./ -type f 查找目录find ./ -type d 查找名字为test的文件或目录find ./ -name test 查找名字符合正则表达式的文件,注意前面的‘.*’(查 ...
- css案例 - 评分效果的星星✨外衣
纳尼?什么星星外衣?好,直接上图比较能说清楚: 仔细看会发现规律:可以根据百分比/分值动态改变高亮星星的个数. 分步骤图: 这种效果,如果遇到一分一个星,没有半星(或者有也可以,直接加一个半星的类名) ...