I have a csv file which containing data where the header contains keys and the cells contain values. I would like to use python to create a yaml file from the contents of the csv file.
I created a dictionary of the K:V pairs; however, I am stuck trying to get the K:V pairs into the yaml file.
The structure of the yaml must be:
key1: value1
key2: value2
key3:
- key4: value4
key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
-
-
---
If I were to manually create these, I would have more than 1000 YAMLs so it's pretty time consuming and unrealistic.
I am looking for any ideas your much more experienced people might have.
I would really like the output to iterate through the dictionary to create a huge listing of YAMLs like below:
key1: value1
key2: value2
key3:
- key4: value4
key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
-
-
---
key1: value1
key2: value2
key3:
- key4: value4
key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
-
-
---
key1: value1
key2: value2
key3:
- key4: value4
key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
-
-
---
key1: value1
key2: value2
key3:
- key4: value4
key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
-
-
---
Sample Code:
import csv
import yaml
def csv_dict_list(variables_file) :
reader=csv.DictReader(open(variables_file, 'r'))
dict_list = []
for line in reader:
dict_list.append(line)
return dict_list
yaml_values = csv_dict_list(sys.argv[1])
No matter what I try after this, I can not get the desired output using yaml.load() or yaml.load_all().