pyyaml

How do I install the yaml package for Python?

最后都变了- 提交于 2019-12-17 04:39:45
问题 I have a Python program that uses YAML. I attempted to install it on a new server using pip install yaml and it returns the following: $ sudo pip install yaml Downloading/unpacking yaml Could not find any downloads that satisfy the requirement yaml No distributions at all found for yaml Storing complete log in /home/pa/.pip/pip.log How do I install the yaml package for Python? I'm running Python 2.7. (OS: Debian Wheezy) 回答1: You could try the search feature in pip, $ pip search yaml which

Fastest way to convert JavaScript object/array to Python dict/list

百般思念 提交于 2019-12-13 18:44:49
问题 I'm trying to parse the code of JavaScript objects that hold huge JavaScript arrays and convert it to a Python dictionary with lists. At the moment I'm using PyYaml, but that didn't work directly, as it can't handle consecutive commas (e.g. it breaks on '[,,,0,]' with: expected the node content, but found ',' ). So I substituted these out, but this is all very slow. I'm wondering if any of you know of a better and faster way to do this. JSON decode doesn't work as JavaScript code isn't JSON

UnicodeDecodeError while processing Accented words

筅森魡賤 提交于 2019-12-13 02:58:08
问题 I have a python script which reads a YAML file (runs on an embedded system). Without accents, the script runs normally on my development machine and in the embedded system. But with accented words make it crash with UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128) only in the embedded environment. The YAML sample: data: ã The snippet which reads the YAML: with open(YAML_FILE, 'r') as stream: try: data = yaml.load(stream) Tried a bunch of

remove single quotes from dict values while adding content to yaml file using python ruamel.yaml

ε祈祈猫儿з 提交于 2019-12-12 23:05:11
问题 I have an yaml file as mentioned below test1.yaml resources: name:{get_param: vname} ssh_keypair: {get_param: ssh_keypair} Now I want to add test1_routable_net: { get_param: abc_routable_net } under resources of test1.yaml Here is the code which I tried import ruamel.yaml yaml = ruamel.yaml.YAML() test="{ get_param: abc_routable_net }".strip(‘\’’) with open('/tmp/test1.yaml') as fp: data = yaml.load(fp) data['resources'].update({‘test1_routable_net’:test}) yaml.dump(data,file('/tes2.yaml', 'w

How to parse a YAML file with multiple documents?

浪子不回头ぞ 提交于 2019-12-12 12:05:27
问题 Here is my parsing code: import yaml def yaml_as_python(val): """Convert YAML to dict""" try: return yaml.load_all(val) except yaml.YAMLError as exc: return exc with open('circuits-small.yaml','r') as input_file: results = yaml_as_python(input_file) print results for value in results: print value Here is a sample of the file: ingests: - timestamp: 1970-01-01T00:00:00.000Z id: SwitchBank_35496721 attrs: Feeder: Line_928 Switch.normalOpen: 'true' IdentifiedObject.description: SwitchBank

Serializing namedtuples via PyYAML

柔情痞子 提交于 2019-12-12 09:58:31
问题 I'm looking for some reasonable way to serialize namedtuples in YAML using PyYAML. A few things I don't want to do: Rely on a dynamic call to add a constructor/representor/resolver upon instantiation of the namedtuple. These YAML files may be stored and re-loaded later, so I cannot rely on the same runtime environment existing when they are restored. Register the namedtuples in global. Rely on the namedtuples having unique names I was thinking of something along these lines: class namedtuple

django: want to have a form for dynamically changed sequence data

折月煮酒 提交于 2019-12-12 04:52:26
问题 Django experts - I am a newbie and need your help with the following. Suppose I have some complicated data structure, an arbitrary example: (yaml format) foo: { ff: [ bar, foobar] bb: {'some map values'} } bar: [str!! "", str!! ""] foobar: [str!! "", str!! ""] ... My goal is a web interface which allows to create/modify/save and display such data. I can't imagine how to define a form and a model for such kind of data. The problem is that the data is not static, for example the user can add as

Copy content from one YAML to another YAML after comparison of keys

匆匆过客 提交于 2019-12-12 02:08:12
问题 I have a use case where I need to pick key:value pairs from a new YAML file. Check whether that key exists in old YAML file and if it does copy the value and set it in new YAML file. Also if that key does not exist in old file then ask the user. Code: copyfile('all.isv', '/home/ubuntu/tmp/deploy/all') with open("/home/ubuntu/ansible-environments/aws/lp/all", 'r') as f1: try: oldvars = yaml.load(f1) with open("/home/ubuntu/tmp/deploy/all", 'rw') as f2: newvars = yaml.load(f2) for key,value in

merge two yaml files in python

不羁的心 提交于 2019-12-11 15:48:40
问题 I have two yaml files as mentioned below test1.yaml resources: server_group_1: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 5] } policies: [ { get_param: [server_group_types, 5] } ] server_group_2: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 8] } policies: [ { get_param: [server_group_types, 8] } ] output: check_1: description: Name of the instance value: { get_attr: [check_1, vname] } test2.yaml resources: server_group_4: type: OS:

How to make a custom YAML tag work with a sequence alias in pyyaml

拥有回忆 提交于 2019-12-11 07:53:30
问题 I’ve got a yaml file with aliases like this: vars: users: &users ['user1', 'user2', 'user3', 'user4'] refs: users: *users user: !rand ['user1', 'user2', 'user3', 'user4'] !rand is a custom tag that calls python’s random.choice on a yaml sequence. I’m using the following implementation of the !rand tag: import random import yaml def load_yaml(file): def rand_constructor(loader, node): value = loader.construct_sequence(node) return random.choice(value) yaml.add_constructor('!rand', rand