我是Python的新手并尝试阅读YAML文件。根据其内容,我想创建Python对象。我正在使用ruamel-yaml库。 在我的例子中,也许我有Python-Class Message,Signal和Signalgroup等。(参见示例文件)。
我的方法是读取YAML文件,检查每一行的给定关键字并创建相关对象并用数据填充它。我认为这是“旧学校”的方法,也许有一种更有效的方法来处理文件。
也许使用函数register_class / rep。创建标签“from_yaml”但是当键被索引时,它将不起作用。
Message1:
Message2:
Message3:
有没有更专业的方法?
# Yaml Testfile
- ModuleName: myTestModule
- Version: 1.0
- ModuleNumbers: [96,97,98,99]
- Message1:
Name: AO3_
DLC: 8
Signal1:
Name: Temperature
Length: 16
Signal2:
Name: AnalogOut3
Length: 16
SignalGroup1: #Comment
Name: app_fcex
Type: Bitfield
Signal1:
Name: drive_ready
Length: 1
Signal2:
Name: error_active
Length: 1
Signal3:
Name: warning_active
Length: 1
Signal3:
Name: Temperatur 2
Length: 8
ValueTable:
Name: TempStates
Item0:
Name: INIT
Value: 1
Item1:
Name: RUN
Value: 2
Item2:
Name: DONE
Value: 3
Item3:
Name: ERROR
Value: 4
- Message2:
name: AO2_
object: RX2
DLC: 8
答案 0 :(得分:0)
我建议您在YAML文件中使用标记,并放弃使用名称为Item1
,Item2
的键(替换为标记对象列表)。
很难看到数据的确切结构,但最初的步骤可能是制作YAML文档(假设在文件input.yaml
中:
- ModuleName: myTestModule
- Version: 1.0
- ModuleNumbers: [96,97,98,99]
- !Message
Name: AO3_
DLC: 8
Signal1:
Name: Temperature
Length: 16
Signal2:
Name: AnalogOut3
Length: 16
SignalGroup1: #Comment
Name: app_fcex
Type: Bitfield
Signal1:
Name: drive_ready
Length: 1
Signal2:
Name: error_active
Length: 1
Signal3:
Name: warning_active
Length: 1
Signal3:
Name: Temperatur 2
Length: 8
ValueTable:
Name: TempStates
items:
- !Item
Name: INIT
Value: 1
- !Item
Name: RUN
Value: 2
- !Item
Name: DONE
Value: 3
- !Item
Name: ERROR
Value: 4
- !Message
name: AO2_
object: RX2
DLC: 8
并加载:
import sys
import ruamel.yaml
class Item:
def __init__(self, name=None, value=None):
self.name = name
self.value = value
@classmethod
def from_yaml(cls, constructor, node):
for m in constructor.construct_yaml_map(node):
pass
return cls(m['Name'], m['Value'])
def __repr__(self):
return 'Item(name={.name}, value={.value})'.format(self, self)
class Message:
def __init__(self, name=None, DLC=None, object=None, signals=None):
self.name = name
self.dlc = DLC
self.object = object
self.signals = [] if signals is None else signals
@classmethod
def from_yaml(cls, constructor, node):
for m in constructor.construct_yaml_map(node):
pass
if 'Name' in m:
name = m['Name']
elif 'name' in m:
name = m['name']
else:
name = None
object = m['object'] if 'object' in m else None
if 'DLC' in m:
dlc = m['DLC']
else:
dlc = None
if 'signals' in m:
signals = m['signals']
elif 'Signal1' in m:
x = 1
signals = []
while True:
name = "Signal{}".format(x)
try:
signals.append(m[name])
except KeyError:
break
x += 1
else:
signals = None
return cls(name, dlc, object, signals)
def __repr__(self):
return 'Message(name={}, DLC={}, object={}, signals{})'.format(
self.name, self.dlc, self.object, '[...]' if self.signals else '[]',
)
yaml = ruamel.yaml.YAML(typ='safe')
yaml.register_class(Item)
yaml.register_class(Message)
with open('input.yaml') as fp:
data = yaml.load(fp)
以上内容包含一些但有限的关键可用性检查(例如Name
的{{1}}和name
标准化。
用上面的!Message
给出(手工完成包装):
print('data')
和[{'ModuleName': 'myTestModule'},
{'Version': 1.0},
{'ModuleNumbers': [96, 97, 98, 99]},
Message(name=Signal4, DLC=8, object=None, signals[...]),
Message(name=AO2_, DLC=8, object=RX2, signals[])]
给出:
print(data[3].signals[2]['ValueTable']['items'][2])
当然应该酌情添加其他课程。