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 ...
随机推荐
- flexbox父盒子flex-direction属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- /usr/bin/ld: cannot find -lxxx 的解决办法
/usr/bin/ld: cannot find -lxxx 的解决办法 在软件编译过程中,经常会碰到类似这样的编译错误: /usr/bin/ld: cannot find -lhdf5 这表示找不到 ...
- 使用CountDownLatch模拟高并发场景
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...
- RGB24转YUV420
BOOL RGB2YUV(LPBYTE RgbBuf, UINT nWidth, UINT nHeight, LPBYTE yuvBuf, unsigned long *len) { if (RgbB ...
- 给sharepoint某列表项单独赋予权限
/// <summary> /// 列表项事件 /// </summary> public class EventReceiver2 : SPItemEventReceiver ...
- Linux下的/proc目录介绍
proc被称为虚拟文件系统,它是一个控制中心,可以通过更改其中某些文件改变内核运行状态, 它也是内核提空给我们的查询中心,用户可以通过它查看系统硬件及当前运行的进程信息. Linux中许多工具的数据来 ...
- Android5.x新特性之 Toolbar和Theme的使用
Toolbar 你还在为Android 的ActionBar的文字不能随意设置位置而烦恼么?你还在为ActionBar不能自定义添加自己的布局而烦恼么?现在告诉你一个好消息,当你看到这篇文章时,就不必 ...
- C语言EOF是什么?
C语言 EOF是什么? Linux中,在新的一行的开头,按下Ctrl-D,就代表EOF(如果在一行的中间按下Ctrl-D,则表示输出"标准输入"的缓存区,所以这时必须按两次Ctrl ...
- HTML-锚点-JS跳转锚点
window.location.hash使用说明,这篇写的挺详细的 http://www.cnblogs.com/nifengs/p/5104763.html a标签的话是 name,div呢是id, ...
- [原]git的使用(五)---删除文件
9.删除文件 [实践出真知] 创建test.txt 文件 并add 和commit到仓库 $ git status #新增加的文件test.txt On branch master Untracke ...