I have two yaml files names that have similar structure but with different data. I need to parse out the ip and name of each host and put into a single csv (excel) file as three columns.
sample:
instances:
- host: 173.20.1.1
timeout: 1.0
tags:
- ip:173.20.1.1
- env:prod
- type:virtual
- name:2-base
- hardware:server
- host: 174.28.2.2
timeout: 1.0
tags:
- ip:174.28.2.2
- env:prod
- type:virtual
- name:2-game
- hardware:server
- host: 174.28.32.8
timeout: 1.0
tags:
- ip:174.28.32.8
- env:prod
- type:virtual
- name:2-play
- hardware:server
Expected output:
https://i.postimg.cc/nLKrppsv/output-csv-Excel.png
I reviewed similar questions at these links but I'm stumped:
Need a script that extracts from a yaml file content and output as a csv file
Convert several YAML files to CSV
This is current code:
import yaml
import csv
import glob
yaml_file_names = glob.glob('./*.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.safe_load(f)
for each_dict in data['instances']:
for each_nested_dict in each_dict['host']:
for each_option in each_nested_dict['tags']:
#write to csv yaml_file_name, each_nested_dict['tags'], each_option
rows_to_write.append([each_yaml_file, each_nested_dict['ip'], each_option])
rows_to_write.append([each_yaml_file, each_nested_dict['name'], 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")
I get this error:
Processing file 1 of 2 file name: .\conf.yaml
Traceback (most recent call last):
File "C:\NGSC\yaml2csv3.py", line 21, in <module>
for each_option in each_nested_dict['tags']:
builtins.TypeError: string indices must be integers
I know there are a lot of threads on "string indices must be integers", but I haven't been successful with them so far.