Today, using Configparser to parse an INI file, report the following error:
Config.read (logFile) File "C:\Python26\lib\ConfigParser.py", line 286, in read self._read (FP, filename) File "C:\Python26\lib\ConfigParser.py", line 482, in _read raise Missingsectionheadererror (Fpname, Lineno, line) Missingsectionheadererror:file contains no section headers.file:c:\test\test.ini, line:1 ' \xff\xfe\r\x00\n '
The analysis is due to the file encoding method, the adjustment code as follows, the problem solved:
def test (): logFile = R ' C:\test\test.ini ' config = configparser.configparser () CONFIG.READFP ( Codecs.open (LogFile, "R", "Utf_16")) print config.sections ()
This file is Unicode encoded, so fill in "utf_16" here, and modify the parameter if it is in a different encoding format.
Python code query address: http://docs.python.org/2/library/codecs.html?highlight=streamcodec#standard-encodings
ConfigParser.MissingSectionHeaderError:File contains no section headers.