有没有其他的方法,可以将选中的数据 加入 new_list中。
有没有其他的方法,可以将选中的数据 加入 new_list中
有没有其他的方法,可以将选中的数据 加入 new_list中
有没有其他的方法,可以将选中的数据 加入 new_list中
有没有其他的方法,可以将选中的数据 加入 new_list中
求大神教教。
分两部分处理,一部分是req_body,另一部分是req_body以外的。前者继续当json处理。存入new_list是只要值,还是字典?
这样是否满足要求:
# json_data为手输,不完整
json_data = [
{
"req_body": { "scene": "1", "username": "1333", "verify_code": "8888", "password": "123456"},
"status_code": 200,
"status": 1,
"msg": "注册成功"
}
]
new_list = []
for item in json_data:
a = item.get("req_body")
del a["scene"]
new_list.append(a)
new_list.append(item.get("status_code"))
new_list.append(item.get("status"))
new_list.append(item.get("msg"))
print(new_list)
# 输出内容为
# [{'username': '1333', 'verify_code': '8888', 'password': '123456'}, 200, 1, '注册成功']
您这是爬取的文本吧?
文本就是字符串。您直接用 eval() 把 json_data 还原成 Python 列表,您取用列表中嵌套字典中您选中的部分就好。
可以使用字典的values()方法来获取所有的值,然后以列表形式存储,例如:
my_dict = {'a': 1, 'b': 2, 'c': 3}
# 获取字典的值,并以列表形式存储到new_list中
new_list = list(my_dict.values())
print(new_list) # 输出 [1, 2, 3]
这里使用了list()
函数将my_dict.values()
的结果转换为列表形式。这样,就可以更高效、简洁地将字典的数据存储到一个新的列表中了。