Here is my parsing code:
import yaml
def yaml_as_python(val):
"""Convert YAML to dict"""
try:
return yaml.load_all(val)
except yaml.YAMLError as exc:
return exc
with open('circuits-small.yaml','r') as input_file:
results = yaml_as_python(input_file)
print results
for value in results:
print value
Here is a sample of the file:
ingests:
- timestamp: 1970-01-01T00:00:00.000Z
id: SwitchBank_35496721
attrs:
Feeder: Line_928
Switch.normalOpen: 'true'
IdentifiedObject.description: SwitchBank
IdentifiedObject.mRID: SwitchBank_35496721
PowerSystemResource.circuit: '928'
IdentifiedObject.name: SwitchBank_35496721
IdentifiedObject.aliasName: SwitchBank_35496721
loc: vector [43.05292, -76.126800000000003, 0.0]
kind: SwitchBank
- timestamp: 1970-01-01T00:00:00.000Z
id: UndergroundDistributionLineSegment_34862802
attrs:
Feeder: Line_928
status: de-energized
IdentifiedObject.description: UndergroundDistributionLineSegment
IdentifiedObject.mRID: UndergroundDistributionLineSegment_34862802
PowerSystemResource.circuit: '928'
IdentifiedObject.name: UndergroundDistributionLineSegment_34862802
path:
- vector [43.052942000000002, -76.126716000000002, 0.0]
- vector [43.052585000000001, -76.126515999999995, 0.0]
kind: UndergroundDistributionLineSegment
- timestamp: 1970-01-01T00:00:00.000Z
id: UndergroundDistributionLineSegment_34806014
attrs:
Feeder: Line_928
status: de-energized
IdentifiedObject.description: UndergroundDistributionLineSegment
IdentifiedObject.mRID: UndergroundDistributionLineSegment_34806014
PowerSystemResource.circuit: '928'
IdentifiedObject.name: UndergroundDistributionLineSegment_34806014
path:
- vector [43.05292, -76.126800000000003, 0.0]
- vector [43.052928999999999, -76.126766000000003, 0.0]
- vector [43.052942000000002, -76.126716000000002, 0.0]
kind: UndergroundDistributionLineSegment
...
ingests:
- timestamp: 1970-01-01T00:00:00.000Z
id: OverheadDistributionLineSegment_31168454
In the traceback, note that it starts having a problem at the ...
Traceback (most recent call last):
File "convert.py", line 29, in <module>
for value in results:
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/yaml/__init__.py", line 82, in load_all
while loader.check_data():
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/yaml/constructor.py", line 28, in check_data
return self.check_node()
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/yaml/composer.py", line 18, in check_node
if self.check_event(StreamStartEvent):
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/yaml/parser.py", line 174, in parse_document_start
self.peek_token().start_mark)
yaml.parser.ParserError: expected '<document start>', but found '<block mapping start>'
in "circuits-small.yaml", line 42, column 1
What I would like is for it to parse each of these documents as a separate object, perhaps all of them in the same list, or pretty much anything else that would work with the PyYAML module. I believe the ...
is actually valid YAML so I am surprised that it doesn't handle it automatically.