Python configparser读取值中的注释

时间:2018-12-13 10:03:41

标签: python

ConfigParser也读取注释。为什么?这不是“忽略”内联注释的默认设置吗?

我用以下脚本重现了我的问题:

import random
tot1 = 0
tot2 = 0
tot2 = 0
rnd2 = 0

for i in range (1,6):
while True:
from random import randint
    print()
    print("Player 1")
    ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    if ro1_1 == 'roll':
        dice1_1 = (randint(1,6))
        print("Player1 dice 1:", dice1_1)
    else:
        ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    ro1_2 = input("Type 'roll' to roll your 2nd dice: ")
    if ro1_2 == "roll":
        dice1_2 = (randint(1,6))
        print("Player1 Dice 2:", dice1_2)
    else:
        ro1_2 = input("Type 'roll' to roll your 1st dice: ")
    print()
    print ("Player1's total for round",(rnd1)," is:",tot1) 
    print()
    print ("Player 2")
    ro2_1 = input("Type 'roll' to roll your 1st dice: ")
    if ro2_1 == 'roll':
        dice2_1 = (randint(1,6))
        print("Player2 Dice 1:", dice2_1)
    else:
        ro1_1 = input("Type 'roll' to roll your 1st dice: ")
    ro2_2 = input("Type 'roll' to roll your 2nd dice: ")
    if ro2_2 == 'roll':
        dice2_2 = (randint(1,6))
        print("Player2 Dice 2:", dice2_2)
    else:
        ro2_2 = input("Type 'roll' to roll your 1st dice: ")
        break
    print()

print ("Player2's total for round",(rnd2)," is:",tot2)
    print()
    break

ini文件如下所示:

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

输出:

[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" #

我不想读 30; mm ,但我只想读数字'30'。

我在做什么错了?

PS:Python3.7

1 个答案:

答案 0 :(得分:3)

在创建下面的configparser对象检查示例时,请使用inline_comment_prefixes

2018-12-13 10:02:16,704 [9] INFO  Hms.Domain.Concrete.RoomsRepository [(null)] - Test

Here是详细的文档。