[英]How to extract data from json file in python
I have a below json config file in a project.我在项目中有一个低于 json 的配置文件。
"Jackets": [
{
"GreenHSV": [30, 150, 72, 80, 255, 255]
},
{
"OrangeHSV": [0, 150, 72, 25, 255, 255]
}
]
I have to get the each value of GreenHSV
(30, 150, 72, 80, 255, 255)
and OrangeHSV
0, 150, 72, 25, 255, 255
in python code, keeping in mind that in future another HSV can be added like below我必须在 python 代码中获取
GreenHSV
(30, 150, 72, 80, 255, 255)
和OrangeHSV
0, 150, 72, 25, 255, 255
的每个值,记住以后可以添加另一个 HSV像下面
{
"RedHSV": [0, 150, 72, 25, 255, 255]
}
so the code should be able to read RedHSV
values as well without making any changes in the code.因此代码应该也能够读取
RedHSV
值,而无需对代码进行任何更改。 In above JSON, Jackets
is basically a list
which contains dict of list
(if I am not wrong).在上面的 JSON 中,
Jackets
基本上是一个包含list
dict of list
(如果我没记错的话)。 To get each color HSV value, my approach is below:要获得每种颜色的 HSV 值,我的方法如下:
for i in range(len(config["Jackets"])):
print(config["Jackets"][i])
hsv_dict = config["Jackets"][i]
print(hsv_dict.keys())
name = hsv_dict.keys()
hsv_color_name = hsv_dict[name]
print(hsv_color_name[0], hsv_color_name[1], hsv_color_name[2])
print(hsv_color_name[3], hsv_color_name[4], hsv_color_name[5])
My approach is to first put a for
loop with the range
of len
of Jackets
so that if in future, new color is added, it will loop 3 times.我的方法是首先在
Jackets
的len
range
放置一个for
循环,这样如果将来添加新颜色,它将循环 3 次。
After that I have extracted the hsv_dict
which contains this value {'GreenHSV': [30, 150, 72, 80, 255, 255]}
.之后,我提取了包含此值的
hsv_dict
{'GreenHSV': [30, 150, 72, 80, 255, 255]}
。 Now at this point I though I should get the key name of the dict as it will be GreenHSV
or any next color, so that I can extract its value which will be list ([30, 150, 72, 80, 255, 255])
and then I can get the list values easily.现在在这一点上,我虽然应该得到 dict 的键名,因为它将是
GreenHSV
或任何下一个颜色,这样我就可以提取它的值,它将是 list ([30, 150, 72, 80, 255, 255])
然后我可以轻松获取列表值。 But looks like my approach is wrong.但看起来我的方法是错误的。
Can anyone please guide me.任何人都可以请指导我。 Thanks
谢谢
hsv_dict = config["Jackets"][i]
when i
is 0 will cause hsv_dict
to contain {'GreenHSV': [30, 150, 72, 80, 255, 255]}
.当
i
为 0 时,将导致hsv_dict
包含{'GreenHSV': [30, 150, 72, 80, 255, 255]}
。
The problem in your code is that after name = hsv_dict.keys()
, name
contains a dict_keys
object that will yield all of the keys.您的代码中的问题是,在
name = hsv_dict.keys()
之后, name
包含一个dict_keys
object ,它将产生所有密钥。
You need to extract the first key, which requires something like您需要提取第一个密钥,这需要类似
name = list(hsv_dict.keys())[0]
I'm not sure why you're using a list of dictionaries that only have one entry each though.我不确定您为什么要使用一个每个只有一个条目的字典列表。 I feel like the following would be an easier and more concise layout:
我觉得以下将是一个更简单,更简洁的布局:
"Jackets": {
"GreenHSV": [30, 150, 72, 80, 255, 255],
"OrangeHSV": [0, 150, 72, 25, 255, 255]
}
hsv_dict.keys()
returns a dict_keys
object, so you should convert it to a list like so: hsv_dict.keys()
返回一个dict_keys
object,所以你应该把它转换成这样的列表:
list(hsv_dict.keys())
Since this is a list you can get the 0th argument like this:由于这是一个列表,您可以像这样获得第 0 个参数:
list(hsv_dict.keys())[0]
This contains your key value这包含您的关键值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.