字符串列表的语法与unrepr模式

时间:2015-03-08 16:52:45

标签: python configobj

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

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.

1 个答案:

答案 0 :(得分:0)

将unrepr设置为true,您必须指示Electric被赋予字符串。所以,你必须写:

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

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