I am trying to create a dict like this:
{
"Name": "string",
"Info": [
{
"MainID": "00000000-0000-0000-0000-000000000000",
"Values": [
{
"IndvidualID": "00000000-0000-0000-0000-000000000000",
"Result": "string"
},
{
"IndvidualID": "00000000-0000-0000-0000-000000000000",
"Result": "string"
}
]
}
]
}
Where the Values section has 100+ things inside of it. I put 2 as an example.
Unsure how to build this dynamically. Code I have attempted so far:
count = 0
with open('myTextFile.txt') as f:
line = f.readline()
line = line.rstrip()
myValues[count]["IndvidualID"] = count
myValues[count]["Result"] = line
count = count +1
data = {"Name": "Demo",
"Info": [{
"MainID":TEST_SUITE_ID,
"Values":myValues
}}
This does not work however. Due to "Key Error 0" it says. Works if I do it like myValues[count]= count but as soon as I add the extra layer it breaks myValues[count]["IndvidualID"] = count. I see some example of setting a list in there, but I need like a List (Values) with multiple things inside (ID and Result). Have tried a few things with no luck. Anyone have a simple way to do this in python?
Full traceback:
Traceback (most recent call last):
File "testExtractor.py", line 28, in <module>
myValues[count]["IndvidualID"] = count
KeyError: 0
myTextFile.txt, or more specifically, the contents it would need to have in order to produce the json data in your first code block.myValueswithout defining it. Please update your question with the full Traceback of the error you are seeing.