简体   繁体   English

如何将字符串转换为有效的 json 或 yaml

[英]How to convert string to valid json or yaml

I have a large script that parses js with a dataframe entry, but to shorten the question, I put what I need in a separate variable.我有一个大型脚本,它使用 dataframe 条目解析 js,但为了缩短问题,我将我需要的内容放在一个单独的变量中。 My variable contains the following value我的变量包含以下值

value = "{from:[3,4],to:[7,4],color:2},{from:[3,6],to:[10,6],color:3}"

I apply the following script and get data like this我应用以下脚本并获取这样的数据

value = "{from:[3,4],to:[7,4],color:2},{from:[3,6],to:[10,6],color:3}"

def parse_json(value):
    arr = value.split("},")
    arr = [x+"}" for x in arr]
    arr[-1] = arr[-1][:-1]
    
    return json.dumps({str(i):add_quotation_marks(x) for i, x in enumerate(arr)})

def add_quotation_marks(value):
    words = re.findall(r'(\w+:)', value)
    for word in words:
        value = value.replace(word[:-1], f'"{word[:-1]}"')
    return json.loads(value)

print(parse_json(value))
{"0": {"from": [3, 4], "to": [7, 4], "color": 2}, "1": {"from": [3, 6], "to": [10, 6], "color": 3}}

The script executes correctly, but I need to get a slightly different result.脚本正确执行,但我需要得到稍微不同的结果。 This is what the result I want to get looks like:这就是我想要得到的结果:

{
  "0": {
    "from": {
      "0": "3",
      "1": "4"
    },
    "to": {
      "0": "7",
      "1": "4"
    },
    "color": "2"
  },
  "1": {
    "from": {
      "0": "3",
      "1": "6"
    },
    "to": {
      "0": "10",
      "1": "6"
    },
    "color": "3"
  }
}

This is valid json and valid yaml. Please tell me how can I do this这是有效的 json 和有效的 yaml。请告诉我该怎么做

I'd suggest a regex approach in this case:在这种情况下,我建议使用正则表达式方法:

res = []

# iterates over each "{from:...,to:...,color:...}" group separately
for obj in re.findall(r'\{([^}]+)}', value):
    item = {}

    # iterates over each "...:..." key-value separately
    for k, v in re.findall(r'(\w+):(\[[^]]+]|\d+)', obj):
        if v.startswith('['):
            v = v.strip('[]').split(',')

        item[k] = v

    res.append(item)

This produces this output in res :这会在res中产生这个 output :

[{'from': ['3', '4'], 'to': ['7', '4'], 'color': '2'}, {'from': ['3', '6'], 'to': ['10', '6'], 'color': '3'}]

Since your values can contain commas, trying to split on commas or other markers is fairly tricky, and using these regexes to match your desired values instead is more stable.由于您的值可以包含逗号,因此尝试在逗号或其他标记上拆分是相当棘手的,而使用这些正则表达式来匹配您想要的值反而更稳定。

Here's the code that converts the the value to your desired output.下面是将value转换为所需的 output 的代码。

import json5  # pip install json5

value = "{from:[3,4],to:[7,4],color:2},{from:[3,6],to:[10,6],color:3}"

def convert(str_value):
    str_value = f"[{str_value}]"  # added [] to make it a valid json
    parsed_value = json5.loads(str_value)  # convert to python object
    output = {}  # create empty dict

    # Loop through the list of dicts. For each item, create a new dict
    # with the index as the key and the value as the value. If the value
    # is a list, convert it to a dict with the index as the key and the
    # value as the value. If the value is not a list, just add it to the dict.
    for i, d in enumerate(parsed_value):
        output[i] = {}
        for k, v in d.items():
            output[i][k] = {j: v[j] for j in range(len(v))} if isinstance(v, list) else v

    return output

print(json5.dumps(convert(value)))

Output Output

{
  "0": {     
    "from": {
      "1": 4
    },
    "to": {
      "0": 7,
      "1": 4
    },
    "color": 2
  },
  "1": {
    "from": {
      "0": 3,
      "1": 6
    },
    "to": {
      "0": 10,
      "1": 6
    },
    "color": 3
  }
}
  • json5 package allows you to convert a javascrip object to a python dictionary so you dont have to do split("},{") . json5 package 允许您将 javascrip object 转换为 python 字典,因此您不必执行split("},{")
  • Then added [ and ] to make the string a valid json.然后添加[]使字符串成为有效的 json。
  • Then load the string using json5.loads()然后使用json5.loads()加载字符串
  • Now you can loop through the dictionary and convert it to desired output format.现在您可以遍历字典并将其转换为所需的 output 格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM