gpt4 book ai didi

python - 从 JSON 字段中拆分字符串值并分配给 YAML 映射

转载 作者:行者123 更新时间:2023-11-28 17:02:19 25 4
gpt4 key购买 nike

我想将一个字符串拆分成多个部分并将其分配给 YAML 文件中的字段。这是我的 JSON 文件。

{
"App Name": "blah",
"Email": "blah@blah.com",
"Employee ID": "xyz",
"Load Balancing Method": "Round Robin",
"Network": "CMN",
"Pool Member": "pucq-spn-198 5001/tcp\r\npucq-spn-199 5002/tcp\r\npucq-spn-200
5003/tcp",
"Pool Monitor": "tcp",
"Pool name": "pool",
"SSL": "Required",
"VIP Name": "vs-ng"
}

这是用户从网络表单提交的输入,我正在通过 API 访问它。我想将 Pool Member 字段中的值拆分为分隔字符串,例如 pucq-spn-198 5001 pucq-spn-199 5002 并将它们分配给 YAML 文件中的不同字段。这是我的 YAML 文件。

nodes:
- description: PU
host: 10.10.10.10
monitors:
- /Common/icmp
name: node
- description: PU
host: 10.10.10.10
monitors:
- /Common/icmp
name: node-puex
partition: Common
pool:
descriptions: PUE
lb_method: Round Robin
monitors:
- /Common/tcp
name: pool
pool_members:
- node_name: **pucq-spn-198**
port: **5001**
- node_name: **pucq-spn-199**
port: **5002**
server: gmly-p-01.blah.com
virtual_server:
destination: 1.1.1.1
ip_protocol: udp
name: vs-ng
pool: pool-pu-ind
port: 999
profiles:
- name: fastL4
type: performance-l4

我想将值放在 -node_name:pool_members: 下的 port 中。到目前为止,我尝试过这样的事情。但它不起作用。

import json
import requests
import yaml

url = "http://127.0.0.1:5000/vip7.json"
r = requests.get(url)
json_file = json.loads(r.content)
j = json_file

with open('C:/pytest/vs-test.yml', 'r') as f:
y = yaml.load(f)

# Assign JSON values to YAML

y['pool']['lb_method'] = j['Load Balancing Method']
y['pool']['name'] = j['Pool name']
y['virtual_server']['name'] = j['VIP Name']

y['pool_members']['node_name'] = j[0]['Pool Member']
print (y['pool_members']['node_name'])

什么都不做。

也尝试过拆分。它打印出 pucq-spn-198 部分。

pool = j['Pool Member']
p = pool.split()
y['pool_members']['node_name'] = pool[0:12]
print (pool[0:12])

但是当我像这样分配它时 y['pool_members']['node_name'] = pool[0:12] 它不起作用。

最佳答案

我不确定您要强调的确切问题,但是

  1. y['pool_members'] = pool_members 应该产生东西

  2. 我认为 [0:12] 在输出中只需要 3 元素时是不正确的,而且只有 <拆分给定成员字符串值后的 strong>6 个元素。

如果您想迭代值对,一种方法是创建一个生成器函数。

此处显示了您应该执行的操作的完整示例:

import json
import yaml
from pprint import pprint

# defining the input data
data = {
"App Name": "blah",
"Email": "blah@blah.com",
"Employee ID": "xyz",
"Load Balancing Method": "Round Robin",
"Network": "CMN",
"Pool Member": "pucq-spn-198 5001/tcp\r\npucq-spn-199 5002/tcp\r\npucq-spn-200 5003/tcp",
"Pool Monitor": "tcp",
"Pool name": "pool",
"SSL": "Required",
"VIP Name": "vs-ng"
}

# Parse the input data and make sure there are even names and ports after a split
pool_members = data['Pool Member'].split()
amt = len(pool_members)
if amt % 2 != 0:
raise Exception("Not all pool members have a name and port!")
# create a lazy iterator over the list
gen = (pm for pm in pool_members)
# Creates a list of key-value pairs
pool_members = [{"node_name": next(gen), "port" : next(gen)} for _ in range(amt // 2)]

# Generating the YAML
'''
loading a yaml file just makes a dict, so pretend there
was previously any pool members before, or an empty list
'''
yaml_data = {"pool_members": []}
print(3 * "-")
yaml_data["pool_members"] = pool_members
# TODO: Write YAML to file instead of console
print(yaml.dump(yaml_data, default_flow_style=False))

输出如下:

---
pool_members:
- node_name: pucq-spn-198
port: 5001/tcp
- node_name: pucq-spn-199
port: 5002/tcp
- node_name: pucq-spn-200
port: 5003/tcp

关于python - 从 JSON 字段中拆分字符串值并分配给 YAML 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com