英文:
Finding specific words in JSON using python
问题
with open("signal_procedure.json", "r") as file:
analogOut.procedure = json.load(file)
print(type(analogOut.procedure))
# 获取测试用例
listof_cases = []
index = 0
for index in analogOut.procedure.keys():
listof_cases.insert(len(listof_cases), index)
# 获取测试用例的内容
listof_content = []
index = 0
for index in analogOut.procedure.values():
listof_content.insert(len(listof_content), index)
index = 0
x = 0
voltage_values = []
for x in listof_content:
if "Applied Voltage" in x:
voltage_values.index(len(voltage_values), x.values())
print(voltage_values)
Note: The code you provided contains HTML entities like "
which represent double quotes ("), and these entities should be replaced with actual double quotes for the code to work correctly. I've left them as is in the translated code, but you may need to replace them in your actual code.
英文:
I am writing automation script that parses a word document. I turn the keywords and their values into JSON object. But I can't get the specific values from there. I want to get applied voltage values from each and put them into a list.
Here is my JSON object: (just the beginning of it there are 19 test cases)
{
"Test Case 1": [
{
"Test Case": "Test Case 1 : SENSOR_VCC Sinyal İşlemleri ",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-9",
"Applied Voltage": "4",
"Expected Result": 5.0,
"Tolerance": 0.3
},
{
"Port": "J7-9",
"Applied Voltage": "3.3",
"Expected Result": 4.125,
"Tolerance": 0.3
},
{
"Port": "J7-9",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3
}
]
}
],
"Test Case 2": [
{
"Test Case": "Test Case 2 : REF_0V Sinyal İşlemleri ",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-15",
"Applied Voltage": "5",
"Expected Result": 5.0,
"Tolerance": 0.3
},
{
"Port": "J7-15",
"Applied Voltage": "3.3",
"Expected Result": 3.3,
"Tolerance": 0.3
},
{
"Port": "J7-15",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3
}
]
}
],
"Test Case 3": [
{
#.... so on
Here is my code: (I was successfull until I tried to have voltage_values list.)
with open("signal_procedure.json", "r") as file:
analogOut.procedure = json.load(file)
print(type(analogOut.procedure))
#Getting test cases
listof_cases = []
index = 0
for index in analogOut.procedure.keys():
listof_cases.insert(len(listof_cases), index)
#getting values of the test case dictionary
listof_content = []
index = 0
for index in analogOut.procedure.values():
listof_content.insert(len(listof_content), index)
index = 0
x = 0
voltage_values = []
for x in listof_content:
if "Applied Voltage" in x:
voltage_values.index(len(voltage_values), x.values())
print(voltage_values)
答案1
得分: 0
以下是您要求的代码部分的翻译:
dct = {
"Test Case 1": [
{
"Test Case": "Test Case 1 : SENSOR_VCC Signal Processing",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-9",
"Applied Voltage": "4",
"Expected Result": 5.0,
"Tolerance": 0.3,
},
{
"Port": "J7-9",
"Applied Voltage": "3.3",
"Expected Result": 4.125,
"Tolerance": 0.3,
},
{
"Port": "J7-9",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3,
},
],
}
],
"Test Case 2": [
{
"Test Case": "Test Case 2 : REF_0V Signal Processing",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-15",
"Applied Voltage": "5",
"Expected Result": 5.0,
"Tolerance": 0.3,
},
{
"Port": "J7-15",
"Applied Voltage": "3.3",
"Expected Result": 3.3,
"Tolerance": 0.3,
},
{
"Port": "J7-15",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3,
},
],
}
],
}
out = {}
for k, v in dct.items():
out[k] = [[dd.get("Applied Voltage") for dd in d["Sub_Tests"]] for d in v]
print(out)
输出:
{
"Test Case 1": [["4", "3.3", "0"]],
"Test Case 2": [["5", "3.3", "0"]]
}
英文:
IIUC you can do:
dct = {
"Test Case 1": [
{
"Test Case": "Test Case 1 : SENSOR_VCC Sinyal İşlemleri ",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-9",
"Applied Voltage": "4",
"Expected Result": 5.0,
"Tolerance": 0.3,
},
{
"Port": "J7-9",
"Applied Voltage": "3.3",
"Expected Result": 4.125,
"Tolerance": 0.3,
},
{
"Port": "J7-9",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3,
},
],
}
],
"Test Case 2": [
{
"Test Case": "Test Case 2 : REF_0V Sinyal İşlemleri ",
"Test Step": "Test Step 1",
"Sub_Tests": [
{
"Port": "J7-15",
"Applied Voltage": "5",
"Expected Result": 5.0,
"Tolerance": 0.3,
},
{
"Port": "J7-15",
"Applied Voltage": "3.3",
"Expected Result": 3.3,
"Tolerance": 0.3,
},
{
"Port": "J7-15",
"Applied Voltage": "0",
"Expected Result": 0.0,
"Tolerance": 0.3,
},
],
}
],
}
out = {}
for k, v in dct.items():
out[k] = [[dd.get("Applied Voltage") for dd in d["Sub_Tests"]] for d in v]
print(out)
Prints:
{
"Test Case 1": [["4", "3.3", "0"]],
"Test Case 2": [["5", "3.3", "0"]]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论