This might help:
import yaml
import csv
yaml_file_names = ['data.yaml', 'data2.yaml']
rows_to_write = []
for idx, each_yaml_file in enumerate(yaml_file_names):
print("Processing file ", idx+1, "of", len(yaml_file_names), "file name:", each_yaml_file)
with open(each_yaml_file) as f:
data = yaml.load(f)
for each_dict in data['degrees']:
for each_nested_dict in each_dict['electiveGroups']:
for each_option in each_nested_dict['options']:
# write to csv yaml_file_name, each_nested_dict['label'], each_option
rows_to_write.append([each_yaml_file, each_nested_dict['label'], each_option])
with open('output_csv_file.csv', 'w') as out:
csv_writer = csv.writer(out, delimiter='|')
csv_writer.writerows(rows_to_write)
print("Output file output_csv_file.csv created")
Tested this code with two mock input yaml's data.yaml
and data2.yaml
, whose contents were these:
data.yaml
:
code: 9313
degrees:
- name: Design
coreCourses:
- ABCD1
- ABCD2
- ABCD3
electiveGroups: #this is the section i need to extract
- label: Electives
options:
- Studio1
- Studio2
- Studio3
- label: OtherElectives
options:
- Class1
- Development2
- lateclass1
specialisations:
- label: Honours
and data2.yaml
:
code: 9313
degrees:
- name: Design
coreCourses:
- ABCD1
- ABCD2
- ABCD3
electiveGroups: #this is the section i need to extract
- label: Electives
options:
- Studio1
- label: E2
options:
- Class1
specialisations:
- label: Honours
and the output csv file generated was this:
data.yaml|Electives|Studio1
data.yaml|Electives|Studio2
data.yaml|Electives|Studio3
data.yaml|OtherElectives|Class1
data.yaml|OtherElectives|Development2
data.yaml|OtherElectives|lateclass1
data2.yaml|Electives|Studio1
data2.yaml|E2|Class1
and btw, the yaml input that you gave along with your question, it's last 2 lines were not properly indented
And as you said that you needed to parse 300 yaml files in a directory, well, you can use glob
module of python for that, like this:
import yaml
import csv
import glob
yaml_file_names = glob.glob('./*.yaml')
# yaml_file_names = ['data.yaml', 'data2.yaml']
rows_to_write = []
for idx, each_yaml_file in enumerate(yaml_file_names):
print("Processing file ", idx+1, "of", len(yaml_file_names), "file name:", each_yaml_file)
with open(each_yaml_file) as f:
data = yaml.load(f)
for each_dict in data['degrees']:
for each_nested_dict in each_dict['electiveGroups']:
for each_option in each_nested_dict['options']:
# write to csv yaml_file_name, each_nested_dict['label'], each_option
rows_to_write.append([each_yaml_file, each_nested_dict['label'], each_option])
with open('output_csv_file.csv', 'w') as out:
csv_writer = csv.writer(out, delimiter='|', quotechar=' ')
csv_writer.writerows(rows_to_write)
print("Output file output_csv_file.csv created")
Edit: as you asked in comments for skipping those yaml
files where there is no electiveGroup
section, here is the updated program:
import yaml
import csv
import glob
yaml_file_names = glob.glob('./*.yaml')
# yaml_file_names = ['data.yaml', 'data2.yaml']
rows_to_write = []
for idx, each_yaml_file in enumerate(yaml_file_names):
print("Processing file ", idx+1, "of", len(yaml_file_names), "file name:", each_yaml_file)
with open(each_yaml_file) as f:
data = yaml.load(f)
for each_dict in data['degrees']:
try:
for each_nested_dict in each_dict['electiveGroups']:
for each_option in each_nested_dict['options']:
# write to csv yaml_file_name, each_nested_dict['label'], each_option
rows_to_write.append([each_yaml_file, each_nested_dict['label'], each_option])
except KeyError:
print("No electiveGroups or options key found in", each_yaml_file)
with open('output_csv_file.csv', 'w') as out:
csv_writer = csv.writer(out, delimiter='|', quotechar=' ')
csv_writer.writerows(rows_to_write)
print("Output file output_csv_file.csv created")