python3使用configparser读取带有中文的配置文件出现UnicodeDecodeError错误
来源:互联网 发布:中邮网络培训学院 编辑:程序博客网 时间:2024/05/01 15:51
问题:
使用configparser读取配置文件时,出现UnicodeDecodeError错误,在没有任何中文的时候读取可以的,加了1句中文注释后就抛出异常,异常如下:
def readconfig(filename): cf = configparser.ConfigParser() try: configcontent = [] cf.read(filename) sections = cf.sections() for s in sections: configcontent.append(cf.items(s)) except: print("throw a exception:\n{e}".format(e=sys.exc_info())) return None else: return configcontent
配置文件如下:
# -*- coding: utf-8 -*-# 配置文件键值对中如果要表示%,请用"%%"代替"%"[http://meeting.sciencenet.cn]way = getbase_url = "http://meeting.sciencenet.cn/index.php?s=%%2FCategory%%2Findex&cid=5&p="begin = 1end = 10tableSelector = #meetinglist_left > div:nth-child(2) > div.content680 > table:nth-child(1)row = trrowSelector = tr:gt(1)column = tdhref = 1cnName = 1enName = nulltag = nulllocation = 3sponsor = nullstartDate = 5
解决方法
注意
对于有BOM(如Windows下用记事本指定为utf-8)的文件,需要使用 utf-8-sig,即把encoding=utf-8
改为 encoding= utf-8-sig
参考链接:
http://www.07net01.com/2015/03/780710.html
阅读全文
0 0
- python3使用configparser读取带有中文的配置文件出现UnicodeDecodeError错误
- python3配置文件模块configparser.ConfigParser
- 使用python的ConfigParser模块方便的读取配置文件
- Python3 配置文件(configparser)
- Python3 配置文件(configparser)
- Python3 configparser读取含有中文配置config.ini(Windows)显示乱码的解决方法
- Python中使用中文出现UnicodeDecodeError的原理和解决方法
- Python3中urllib的UnicodeDecodeError错误
- ConfigParser 读取 config 配置文件
- python读取配置文件configparser
- Learning Python 015 Python3解决问题:读取文件时,出现乱码或者“UnicodeDecodeError 'gbk' codec can't decode” 错误
- 关于使用pyinstaller 打包带有matplotlib 等出现UnicodeDecodeError解决方法
- 关于使用pyinstaller 打包带有matplotlib 等出现UnicodeDecodeError解决方法
- python3使用pickle读取文件提示TypeError或者UnicodeDecodeError的解决办法
- python3使用pickle读取文件提示TypeError或者UnicodeDecodeError的解决办法
- python3使用pickle读取文件提示TypeError或者UnicodeDecodeError的解决办法
- 【新手入门】Python下不喜欢使用ConfigParser去读取配置文件的Java developer
- 备忘:yum出现UnicodeDecodeError错误的解决办法
- canvas 简单的小球抛物线实现和简单的应用
- 简单使用spring
- 关于Ubuntu下Komodo如何运行
- C++中数字与字符串之间的转换
- Android NDK图形API篇
- python3使用configparser读取带有中文的配置文件出现UnicodeDecodeError错误
- JAVA 对象的初始化过程和类的初始化过程
- 内存管理:_CrtDumpMemoryLeaks和_CrtSetBreakAlloc
- 【Linux】sleep函数的简单实现--mysleep
- 算法——在安卓中实现对前一个星期或后一个星期日期的获取
- JavaMelody使用和监控报告解读
- 详解Java中的Future、FutureTask的原理以及与线程池的搭配使用
- 2017.7.3——2 Tips
- BZOJ 2144 LCA 解题报告