bas
bas

Reputation: 14912

Python configparser reads comments in values

ConfigParser also reads comments. Why? Shouldn't this be a default thing to "ignore" inline comments?

I reproduce my problem with the following script:

import configparser

config = configparser.ConfigParser()
config.read("C:\\_SVN\\BMO\\Source\\Server\\PythonExecutor\\Resources\\visionapplication.ini")

for section in config.sections():
    for item in config.items(section):
        print("{}={}".format(section, item))

The ini file looks as follows:

[LPI]
reference_size_mm_width     =   30 ;mm
reference_size_mm_height    =   40 ;mm 
print_pixel_pitch_mm        =   0.03525 ; mm
eye_cascade                 =   "TBD\haarcascade_eye.xml" #

The output:

C:\_Temp>python read.py
LPI=('reference_size_mm_width', '30 ;mm')
LPI=('reference_size_mm_height', '40 ;mm')
LPI=('print_pixel_pitch_mm', '0.03525 ; mm')
LPI=('eye_cascade', '"TBD\\haarcascade_eye.xml" #')

I don't want to read 30 ;mm but I want to read just the number '30'.

What am I doing wrong?

PS: Python3.7

Upvotes: 1

Views: 433

Answers (1)

Amit Nanaware
Amit Nanaware

Reputation: 3346

hi use inline_comment_prefixes while creating configparser object check example below

config = configparser.ConfigParser(inline_comment_prefixes = (";",))

Here is detailed documentation.

Upvotes: 4

Related Questions