ConfigParser.MissingSectionHeaderError:File contains no section headers.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.