[issue7519] ConfigParser can't read files with BOM markers

STINNER Victor report at bugs.python.org
Thu Jan 7 02:30:07 CET 2010


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

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)
-------------------------------------

----------
nosy: +haypo

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7519>
_______________________________________


More information about the Python-bugs-list mailing list