How to write yaml file from dictionary using python?

I have a csv file containing data in which the header contains keys and the cells contain values. I would like to use python to create a yaml file from the contents of a csv file.

I created a dictionary of pairs K: V; however, I am stuck trying to get K: V pairs into a yaml file.

The yaml structure should be:

key1: value1
key2: value2
key3:
  -  key4: value4
     key5: {key6: [value6]
key7: value7
key8: value8
key9: value9
  -
  -
---

If I manually created them, I would have had more than 1000 YAML, so this is quite a lot of time and unrealistic.

I am looking for any ideas that your more experienced people may have.

I would really like the result of iterating through the dictionary to create a huge YAML list, as shown 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
  -
  -
---

Code example:

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 to do after that, I cannot get the desired result using yaml.load () or yaml.load_all ().

+4
1

, dump() dump_all(), YAML load().

, CSV - Python 2.7, , , Python 3.6: list of dict csv_dict_list, a list OrderedDict). , PyYAML dict .

YAML , :

 key5: {key6: [value6]

} , :

key9: value9
  -
  - 

:

key9: value9
key10:
  -
  -

key9: 
  - value9
  -

- ( Python, , , - Python).

PyYAML . :

import yaml
print(yaml.dump(dict(x=[dict(a=1, b=2)]), indent=4))

- :

x:
- {a: 1, b: 2}

, PyYAML, Python, ruamel.yaml( : ) :

import sys
import csv
import ruamel.yaml

Dict = ruamel.yaml.comments.CommentedMap

def csv_dict_list(variables_file) :
    reader=csv.reader(open(variables_file, 'r'))
    key_list = None
    dict_list = []
    for line in reader:
        if key_list is None:
            key_list = line
            continue
        d = Dict()
        for idx, v in enumerate(line):
            k = key_list[idx]
            # special handling of key3/key4/key5/key6
            if k == key_list[2]:
                d[k] = []
            elif k == key_list[3]:
                d[key_list[2]].append(Dict([(k, v)]))
            elif k == key_list[4]:
                d[key_list[2]][0][k] = dt = Dict()
                dt.fa.set_flow_style()
            elif k == key_list[5]:
                d[key_list[2]][0][key_list[4]][k] = [v]
            else:
                d[k] = v
        dict_list.append(d)
    return dict_list

data = csv_dict_list('test.csv')


yaml = ruamel.yaml.YAML()
yaml.indent(sequence=4, offset=2)
yaml.dump_all(data, sys.stdout)

test.csv:

key1,key2,key3,key4,key5,key6,key7,key8,key9
value_a1,value_a2,value_a3,value_a4,value_a5,value_a6,value_a7,value_a8,value_a9
value_b1,value_b2,value_b3,value_b4,value_b5,value_b6,value_b7,value_b8,value_b9

:

key1: value_a1
key2: value_a2
key3:
  - key4: value_a4
    key5: {key6: [value_a6]}
key7: value_a7
key8: value_a8
key9: value_a9
---
key1: value_b1
key2: value_b2
key3:
  - key4: value_b4
    key5: {key6: [value_b6]}
key7: value_b7
key8: value_b8
key9: value_b9

Python 2.7, Python 3.6

+1

Source: https://habr.com/ru/post/1693286/


All Articles