0

它适用于数字列表;我尝试使用单引号和双引号但没有成功。

from configobj import ConfigObj, ConfigObjError
from io import StringIO

specs= """\
Number= [6, 8]
Electric= [`battery`, `solar_panel`]
"""

car1="""\
Number= 6
Electric= battery
"""

car2= """\
Number= 8
Electric= solar_panel
"""

class Processing():
    def __init__(self, car):
        super().__init__()
        try:
            ConfigObj(StringIO(car), configspec= StringIO(specs), unrepr= True)
        except ConfigObjError as e:
            print(e)

if __name__ == "__main__":
    Processing(car1)
    Processing(car2)

结果:

Parse error from unrepr-ing value at line 2.  
Parse error from unrepr-ing value at line 2.
4

1 回答 1

0

在 unrepr 设置为 true 的情况下,您必须指示给 Electric 一个字符串。所以,你必须写:

car1="""\
Number= 6
Electric= 'battery'
"""

car2= """\
Number= 8
Electric= 'solar_panel'
"""
于 2017-07-07T06:14:09.103 回答