This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ConfigParser can't read files with BOM markers
Type: behavior Stage:
Components: Library (Lib), Unicode Versions: Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Python3: guess text file charset using the BOM
View: 7651
Assigned To: Nosy List: AndrewZiem, BreamoreBoy, brett.cannon, grego87, lukasz.langa, nirai, vstinner
Priority: normal Keywords:

Created on 2009-12-15 19:29 by grego87, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
config.ini grego87, 2009-12-15 19:29 config example
Messages (4)
msg96457 - (view) Author: (grego87) Date: 2009-12-15 19:29
If config.ini file is encoded with utf-8 with bom markers this code

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.ini")
config.sections()

throws:

ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: config.ini, line: 1
'\xef\xbb\xbf[section]\n'

ConfigParser should be able to read such files.
msg97335 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-01-07 01:30
Use utf_8_sig charset and open your file using io.open() or codecs.open() to get unicode sections, options and values.

Example:
-------------------------------------
from ConfigParser import ConfigParser
import io

# create an utf-8 .ini file with a BOM marker
with open('bla.ini', 'wb') as fp:
    fp.write(u'[section]\ncl\xe9=value'.encode('utf_8_sig'))

# read the .ini file
config = ConfigParser()
with io.open('bla.ini', 'r', encoding='utf_8_sig') as fp:
    config.readfp(fp)

# dump the config
for section in config.sections():
    print "[", repr(section), "]"
    for option in config.options(section):
        value = config.get(section, option)
        print "%r=%r" % (option, value)
-------------------------------------
msg111415 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-07-24 02:09
This is a sign of a broaded issue and should either be closed as invalid or superseeded by the main BOM issue.

Brett, I would close it.
msg111529 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-25 09:09
Closing as the main BOM issue is addressed on #7651 and a solution to the OP's problem is given in msg97335.
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51768
2010-09-24 04:52:12AndrewZiemsetnosy: + AndrewZiem
2010-07-25 09:09:02BreamoreBoysetstatus: open -> closed

nosy: + BreamoreBoy
messages: + msg111529

superseder: Python3: guess text file charset using the BOM
resolution: duplicate
2010-07-24 02:09:41lukasz.langasetnosy: + brett.cannon, lukasz.langa
messages: + msg111415
2010-01-07 01:30:05vstinnersetnosy: + vstinner
messages: + msg97335
2009-12-26 00:06:56benjamin.petersonsettitle: CompileParser can't read files with BOM markers -> ConfigParser can't read files with BOM markers
2009-12-25 07:41:54niraisetnosy: + nirai
2009-12-15 19:29:16grego87create