zoukankan      html  css  js  c++  java
  • ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError

    使用ConfigParser来读取配置文件,经常会发现经过记事本、notepad++修改后的配置文件读取时出现下面的问题: 

    ConfigParser.MissingSectionHeaderError: File contains no section headers.
    file: ../conf/mal_crawler_allcids.conf, line: 1
    'xefxbbxbf[basic_config] '


    调试程序后发现文件头部被追加了信息:xefxbbxbf,然后ConfigParser解析出错 
    google了下xefxbbxbf,原因:在window下面用记事本、notepad等编辑文件的时候,如果保存为UNICODE或UTF-8,分别会在文件的开头加上两个字节“xFFxFE”和三个字节“xEFxBBxBF”。 即:BOM


    解决方法:配置文件使用前,去掉这些BOM字节

    def remove_BOM(config_path):
      content = open(config_path).read()
      content = re.sub(r"xfexff","", content)
      content = re.sub(r"xffxfe","", content)
      content = re.sub(r"xefxbbxbf","", content)
      open(config_path, 'w').write(content)

    是不是这个问题,请拿走不谢。

  • 相关阅读:
    HDOJ1024(最大M子段和)
    HDOJ1025(最长上升子序列)
    HDOJ1022(模拟栈)
    HDOJ(1018)
    HDOJ1238(string)
    HDOJ1015(简单深搜)
    HDOJ1016(标准dfs)
    Tabbar视图切换,返回上一视图,添加item
    页面转换方法
    网络状态判断
  • 原文地址:https://www.cnblogs.com/sunyllove/p/11250500.html
Copyright © 2011-2022 走看看