ConfigParser读取记事本、notepad++修改后的配置文件会出现:ConfigParser.MissingSectionHeaderError
2014-03-07 10:51
183 查看
使用ConfigParser来读取配置文件,经常会发现经过记事本、notepad++修改后的配置文件读取时出现下面的问题:
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: ../conf/mal_crawler_allcids.conf, line: 1
'\xef\xbb\xbf[basic_config]\r\n'
调试程序后发现文件头部被追加了信息:\xef\xbb\xbf,然后ConfigParser解析出错
google了下\xef\xbb\xbf,原因:在window下面用记事本、notepad等编辑文件的时候,如果保存为UNICODE或UTF-8,分别会在文件的开头加上两个字节“\xFF\xFE”和三个字节“\xEF\xBB\xBF”。 即:BOM
解决方法:配置文件使用前,去掉这些BOM字节
def remove_BOM(config_path):
content = open(config_path).read()
content = re.sub(r"\xfe\xff","", content)
content = re.sub(r"\xff\xfe","", content)
content = re.sub(r"\xef\xbb\xbf","", content)
open(config_path, 'w').write(content)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: ../conf/mal_crawler_allcids.conf, line: 1
'\xef\xbb\xbf[basic_config]\r\n'
调试程序后发现文件头部被追加了信息:\xef\xbb\xbf,然后ConfigParser解析出错
google了下\xef\xbb\xbf,原因:在window下面用记事本、notepad等编辑文件的时候,如果保存为UNICODE或UTF-8,分别会在文件的开头加上两个字节“\xFF\xFE”和三个字节“\xEF\xBB\xBF”。 即:BOM
解决方法:配置文件使用前,去掉这些BOM字节
def remove_BOM(config_path):
content = open(config_path).read()
content = re.sub(r"\xfe\xff","", content)
content = re.sub(r"\xff\xfe","", content)
content = re.sub(r"\xef\xbb\xbf","", content)
open(config_path, 'w').write(content)
相关文章推荐
- ConfigParser读取配置文件出现ConfigParser.MissingSectionHeaderError
- Python读取配置文件:ConfigParser.NoSectionError: No section:
- [Python]解析.ini 文件出错ConfigParser.MissingSectionHeaderError: File contains no section headers.
- Numpy ConfigParser.MissingSectionHeaderError: File contains no section headers.
- ConfigParser MissingSectionHeaderError: File contains no section headers.
- 配置文件操作.如config.php文件的读取修改等操作
- 读取和修改App.config配置文件
- asp.net中配置文件web.config文件的读取和修改
- 配置文件——App.config文件读取和修改
- python ConfigParser 读取配置文件
- ConfigParser 读取 config 配置文件
- python中读取配置文件ConfigParser
- python中利用ConfigParser模块读取配置文件
- python之ConfigParser读取配置文件
- Python configparser修改配置文件
- 使用python的ConfigParser模块方便的读取配置文件
- 配置文件——App.config文件读取和修改
- C#读取App.config配置文件并修改
- python的ConfigParser读取设置配置文件
- 修改配置文件configparser