关于读取配置文件报错:configparser.MissingSectionHeaderError: File contains no section headers.
file: ‘setup.cfg’, line: 1
‘\ufeff[LOG]\n’
把encoding改成None就可以读出来,有时候指定编码方式可能读不出来
from configparser import ConfigParser
class cngFig:
def __init__(self,filename,encoding=None):
self.cf=ConfigParser()
self.cf.read(filename,encoding)
def get_section(self):
return self.cf.sections()
def get_option(self,section):
return self.cf.options(section)
def get_value(self,section,option):
try:
return eval(self.cf.get(section,option))
except:
return self.cf.get(section,option)
if __name__ == '__main__':
s = cngFig('setup.cfg').get_value('LOG', 'formatter')
print(s)