pyyaml

Different YAML array representations

自闭症网瘾萝莉.ら 提交于 2019-12-01 07:39:22
I'm writing a file type converter using Python and PyYAML for a project where I am translating to and from YAML files multiple times. These file are then used by a separate service that I have no control over, so I need to translate back the YAML the same as I originally got it. My original file has sections of the following: key: - value1 - value2 - value3 Which evaluates to {key: [value1,value2,value3]} using yaml.load() . When I translate this back to YAML my new file reads like this: key: [value1,value2,value3] My question is whether these two forms are equivalent as far as the various

Different YAML array representations

江枫思渺然 提交于 2019-12-01 05:31:15
问题 I'm writing a file type converter using Python and PyYAML for a project where I am translating to and from YAML files multiple times. These file are then used by a separate service that I have no control over, so I need to translate back the YAML the same as I originally got it. My original file has sections of the following: key: - value1 - value2 - value3 Which evaluates to {key: [value1,value2,value3]} using yaml.load() . When I translate this back to YAML my new file reads like this: key:

Getting duplicate keys in YAML using Python

别来无恙 提交于 2019-11-30 22:22:36
We are in need of parsing YAML files which contain duplicate keys and all of these need to be parsed. It is not enough to skip duplicates. I know this is against the YAML spec and I would like to not have to do it, but a third-party tool used by us enables this usage and we need to deal with it. File example: build: step: 'step1' build: step: 'step2' After parsing we should have a similar data structure to this: yaml.load('file.yml') # [('build', [('step', 'step1')]), ('build', [('step', 'step2')])] dict can no longer be used to represent the parsed contents. I am looking for a solution in

Getting duplicate keys in YAML using Python

三世轮回 提交于 2019-11-30 18:15:17
问题 We are in need of parsing YAML files which contain duplicate keys and all of these need to be parsed. It is not enough to skip duplicates. I know this is against the YAML spec and I would like to not have to do it, but a third-party tool used by us enables this usage and we need to deal with it. File example: build: step: 'step1' build: step: 'step2' After parsing we should have a similar data structure to this: yaml.load('file.yml') # [('build', [('step', 'step1')]), ('build', [('step',

Reading YAML file with Python results in yaml.composer.ComposerError: expected a single document in the stream

孤者浪人 提交于 2019-11-30 13:34:20
问题 I have a yaml file that looks like --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341570 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341569 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341568 I am able to read this correctly in Perl using YAML but not in python using YAML. It fails with the error: expected a single document in the stream Program: import yaml stram = open("test", "r") print yaml.load(stram) Error: Traceback (most

How to upgrade disutils package PyYAML?

我们两清 提交于 2019-11-30 05:43:25
I was trying to install chatterbot which has a dependency on PyYAML=3.12 . In my Ubuntu machine installed PyYAML version is 3.11. So I used the following command to upgrade PyYAML : sudo -H pip3 install --upgrade PyYAML But it gives the following error: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall. My pip3 version is 10.0.0. How to resolve this? Try using the --ignore-installed flag: sudo -H pip3 install --ignore-installed PyYAML This works because to upgrade a

How to update yaml file using python

谁说胖子不能爱 提交于 2019-11-30 05:04:51
I have a some.yaml file with the below contents. init_config: {} instances: - host: <IP> username: <username> password: <password> The yaml file should be parsed and updated as below. init_config: {} instances: - host: 1.2.3.4 username: Username password: Password How do I parse the values and update them appropriately? The ruamel.yaml package was specifically enhanced (by me starting from PyYAML) to do this kind of round-trip, programmatic, updating. If you start with (please note I removed the extra initial spaces): init_config: {} instances: - host: <IP> # update with IP username: <username

Parsing YAML, return with line number

*爱你&永不变心* 提交于 2019-11-30 03:22:49
I'm making a document generator from YAML data, which would specify which line of the YAML file each item is generated from. What is the best way to do this? So if the YAML file is like this: - key1: item 1 key2: item 2 - key1: another item 1 key2: another item 2 I want something like this: [ {'__line__': 1, 'key1': 'item 1', 'key2': 'item 2'}, {'__line__': 3, 'key1': 'another item 1', 'key2': 'another item 2'}, ] I'm currently using PyYAML, but any other library is OK if I can use it from Python. puzzlet I've made it by adding hooks to Composer.compose_node and Constructor.construct_mapping :

How can I write data in YAML format in a file?

旧街凉风 提交于 2019-11-29 20:13:21
I need to write the below data to yaml file using Python: {A:a, B:{C:c, D:d, E:e}} i.e., dictionary in a dictionary. How can I achieve this? Matthew Trevor import yaml data = dict( A = 'a', B = dict( C = 'c', D = 'd', E = 'e', ) ) with open('data.yml', 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False) The default_flow_style=False parameter is necessary to produce the format you want (flow style), otherwise for nested collections it produces block style: A: a B: {C: c, D: d, E: e} lou Link to the PyYAML documentation showing the difference for the default_flow_style parameter.

ImportError: No module named 'yaml'

一个人想着一个人 提交于 2019-11-29 18:17:35
问题 I have one script in which I am trying to execute python3 env/common_config/add_imagepullsecret.py But, I am getting the following error: [root@kevin]# python3 env/common_config/add_imagepullsecret.py Traceback (most recent call last): File "env/common_config/add_imagepullsecret.py", line 4, in <module> import yaml ImportError: No module named 'yaml' [root@kevin]# pip3 install pyyaml Requirement already satisfied: pyyaml in /usr/lib64/python3.4/site-packages (3.12) [root@kevin]# PyYAML is