MissingSectionHeaderError: File contains no section headers.(configparser)

。_饼干妹妹 提交于 2019-12-24 01:46:09

问题


I'm using configparser in order to read and modify automatically a file conf named 'streamer.conf'. I'm doing this :

import configparser

config = configparser.ConfigParser()
config.read('C:/Users/../Desktop/streamer.conf')

And then it breaks apart with this Error message :

MissingSectionHeaderError: File contains no section headers.
file: 'C:/Users/../Desktop/streamer.conf', line: 1
u'input{\n'

What might be wrong? Any help appreciated.


回答1:


just specify right encoding

config.read(config_file_path, encoding='utf-8-sig')
  • "utf-8-sig" for UTF-8 with BOM
  • "utf-8" for UTF-8 without BOM



回答2:


I got the same error message when I created a pip.conf file. In my case, I had inadvertently created a UTF-8 file with BOM (byte-order marker) instead of a plain UTF-8 file (with no BOM).

So, check to make sure you have a plain text file. If you're not sure, you can open the file in a hex editor and check the first byte(s).




回答3:


ConfigParser parse UTF-8 file with BOM(xef xbb xbf)

    u = open("setting.ini").read().decode("utf-8-sig").encode("utf-8")
    fp = tempfile.TemporaryFile()
    fp.write(u)
    fp.seek(0)

    conf = ConfigParser.ConfigParser()
    conf.readfp(fp)



回答4:


You didn't include streamer.conf, but from the error message, it's not in the right format. configparser is used for parsing "INI" files:

[section1]
setting1 = value
setting2 = value

[section2]
setting3 = value
setting1 = value

etc.



来源:https://stackoverflow.com/questions/39831956/missingsectionheadererror-file-contains-no-section-headers-configparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!