简体   繁体   English

JSon 中的多行逗号分隔列表 - 使用 Python 解析

[英]Multi line comma separated list in JSon -Parse with Python

I have a json file which I was parsing to read some attributes which looks like below.我有一个 json 文件,我正在解析它以读取一些如下所示的属性。

"controls": {
"PS": {
      "name": "Physical Security",
      "level_name": [
        "PS 1",
        "PS 2"
      ],
      "cost": [
        6,
        10
      ],
      "ind_cost": [
        2,
        2
      ],
      "flow": [
        0.55,
        0.325
      ]
    },
.
.
.
}

Previously when I had only one level like only "PS 1" there was no problem and I was reading the values using this code.以前,当我只有一个级别,例如“PS 1”时,没有问题,我正在使用此代码读取值。

 for control in data['controls']: 
        print(control,': ', data['controls'][control]['name']) 

Now, having more than one levels I am having difficulty.现在,有多个级别我遇到了困难。 I tried to split as below.我尝试如下拆分。

 x = data['controls'][control]['name'].split('\n')

or as below或如下

 x = data['controls'][control]['name'].split(',')

or as below或如下

 x = data['controls'][control]['name'].split('\n,')

Each time I get only one item in x, for this example PS.每次我在 x 中只得到一个项目,对于这个例子 PS。

What I want is to be able to get PS 1 and PS 2 in this order.我想要的是能够按此顺序获得 PS 1 和 PS 2。 Later, I will apply it to cost and other things.. Thanks in advance.稍后,我会将其应用到成本和其他方面。在此先感谢。

Ferda费尔达

Instead of using:而不是使用:

 x = data['controls'][control]['name'].split('\n')

Since you used this loop:由于您使用了此循环:

 for control in data['controls']: 

control isn't the name, as control refers to: control 不是名称,因为 control 指的是:

data['controls'][---]

as an object, thus if you want to use it, you should instead use:作为 object,因此如果你想使用它,你应该使用:

x = control['name'].split('\n')

Assuming no other issues in the code, this should work.假设代码中没有其他问题,这应该可以工作。 Let me know if you face any issues with that!如果您遇到任何问题,请告诉我!

Edit 1:编辑1:

Noting the error mentioned, control was told to be a string, however this means one of two things:注意到提到的错误,控制被告知是一个字符串,但这意味着两件事之一:

  1. the JSON isn't parsed correctly due to some error in the format:由于格式中的一些错误,JSON 未正确解析:

Thinking of this, it would mean that control would include all the JSON inside it eg考虑到这一点,这意味着控制将包括其中的所有 JSON 例如

print(control)

Output: "name": "Physical Security",
          "level_name": [
            "PS 1",
            "PS 2"
          ],
          "cost": [
            6,
            10
          ],
          "ind_cost": [
            2,
            2
          ],
          "flow": [
            0.55,
            0.325
          ]
        },

Option 2) You need to re-parse your result选项 2)您需要重新解析结果

I can't think of why/if this would happen, but if you have a valid JSON as your output you might just simply need to re-parse it the same way you parsed the original (I assume you are using the standard JSON libary?)我想不出为什么/是否会发生这种情况,但是如果你有一个有效的 JSON 作为你的 output 你可能只需要像你解析原始文件一样重新解析它(我假设你使用的是标准 Z0ECD111C1D7A28748A lib Z0ECD111C1D7A28748 ?)

It was my mistake, I used name instead of level_name.这是我的错误,我使用 name 而不是 level_name。 The below code solved my problem.下面的代码解决了我的问题。

x = data['controls'][control]['level_name']
        
        
        for control_level in x:

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

相关问题 在Python中从xml解析逗号分隔的列表 - Parse comma separated list from xml in Python 使用Python解析以逗号分隔的电子邮件列表,其格式为“名称” <email> - Parse a comma separated list of emails in Python which are of the format “Name” <email> 从列表和逗号分隔的行创建python字典 - creating a python dictionary from a list and comma separated line 将嵌套 JSON 中逗号分隔的字符串转换为列表 Python - Convert Comma Separated String in Nested JSON to List Python 如何在python中的文件上进行迭代,在该文件中记录是多行且字段之间用逗号分隔,并且记录由空行分隔? - How to iterate on a file in python where records are multi-line with comma separated fields and the records are delimited by an empty line? 使用 python 中的引号解析逗号分隔的 csv 文件 - parse comma separated csv file with quotes in python 在不使用 CSV 或映射的情况下更改 Python 中逗号分隔的多行文本文件中的单个单词 - Changing a single word in an comma-separated multi-line text file in Python without using CSV or mapping 使用Python将JSON单行解析为多行CSV - JSON Single Line Parse to Multi-Line CSV with Python Python字符串列表到1个字符串,用逗号和引号(,&“)分隔 - Python list of strings to 1 string separated by comma & quotes (, & ") 清单中的Python Unnest逗号分隔值 - Python Unnest Comma Separated Values in List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM