I am trying to fetch data from a .cfg
(configuration file) in python but I am always getting the error
ConfigParser.MissingSectionHeaderError: File contains no section headers.
Here is the link for the .cfg file:
I am trying to fetch data from a .cfg
(configuration file) in python but I am always getting the error
ConfigParser.MissingSectionHeaderError: File contains no section headers.
Here is the link for the .cfg file:
Most probably you have parameters without section, e.g.:
name1 = value1
name2 = value2
According documentation there should be a section.
I may suggest the following solutions:
Add section in ini file, e.g:
[default]
name1 = value1
name2 = value2
from configparser import ConfigParser
parser = ConfigParser()
with open('config.ini') as stream:
parser.read_string('[default]\n' + stream.read())
# now you can use it for parameters without section
print(parser.get('default', 'name1'))
The ConfigParser module assumes that config files have section headers. Section headers are used to keep configurations of one type together. Write [Default]
in the first line of your configuration file. This will tell ConfigParser that there is only one header and that is the default.
You are adding JSON file instead of config file in format:
[OPTIONS]
option1 = foo
option2 = bar