1

im trying to edit my Revit.ini File with python however I continue getting the error below. I've been banging my head against the wall for the better portion of the day. Any help is appreciated in this.

I've been working with ConfigParser on python 2.7 and going through their docs here since they seem to be the way to go when trying to work with *.ini files. When i create my own *.ini file to test stuff on, everything works however when i try to run my test on this other *.ini, i get an error.

my code so far

import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('Revit.ini')

my error

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python27\lib\ConfigParser.py", line 305, in read
    self._read(fp, filename)
  File "C:\Python27\lib\ConfigParser.py", line 512, in _read
    raise MissingSectionHeaderError(fpname, lineno, line)
MissingSectionHeaderError: File contains no section headers.
file: Revit.ini, line: 1
'\xff\xfe\r\x00\n'

Any and all help is appreciated. I think it has something to do with the UTF encoding(which im not at all familiar with yet. i intend to do some reading on it) which then means i probably have to decode it and then re-encode it which i also need to read up on what to do.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Cflux
  • 1,423
  • 3
  • 19
  • 39
  • Possible duplicate of [ConfigParser with Unicode items](https://stackoverflow.com/questions/1648517/configparser-with-unicode-items) – Gord Thompson Jul 16 '19 at 13:51

1 Answers1

0

I think it has something to do with the UTF encoding

Yes, it does. The error message shows the first two bytes of the file as \xff\xfe, which is the byte-order mark (BOM) for a text file containing Unicode characters encoded as UTF-16LE (little-endian). The config parser needs a little help in understanding such files, so we can ask the io module to lend a hand:

config = configparser.ConfigParser()
with io.open('revit.ini', mode='r', encoding='utf-16') as fp:
    config.read_file(fp)

Note that the above is for Python_3. Python_2 is less than six months from end-of-life (January 2020), after which a lot of packages are expected to drop support for it, so you really should consider transitioning to Python_3.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418