简体   繁体   中英

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

hi use inline_comment_prefixes while creating configparser object check example below

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

Here is detailed documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM