0

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
6
  • Please update your question with the reason that 'This does not work however`. Commented Feb 21, 2020 at 14:29
  • Please provide the contents of myTextFile.txt, or more specifically, the contents it would need to have in order to produce the json data in your first code block. Commented Feb 21, 2020 at 14:31
  • @kevin Its just text lines. Such as: Apple, another apple, another another apple (where comma is new line, hard to type comment. Commented Feb 21, 2020 at 14:31
  • I imagine from the code you have posted you are getting an error. You refer to myValues without defining it. Please update your question with the full Traceback of the error you are seeing. Commented Feb 21, 2020 at 14:32
  • If your file contains only "apple" and "another apple", etc, then what does that have to do with the output you want? I don't see "apple" anywhere in your desired output. Commented Feb 21, 2020 at 14:33

1 Answer 1

1

If I add a few bits and pieces I can get this code to run:

count = 0
myValues = []
with open('myTextFile.txt') as f:
    for line in f:
        line = line.rstrip()
        d = {"IndvidualID":count, "Result":line}
        myValues.append(d)
        count = count + 1

TEST_SUITE_ID = 1
data = {"Name": "Demo",
       "Info": [{
          "MainID":TEST_SUITE_ID,
          "Values":myValues
       }]}

Output:

{'Name': 'Demo', 'Info': [{'MainID': 1, 
      'Values': [{'IndvidualID': 0, 'Result': 'apples'}, {'IndvidualID': 1, 'Result': 'another apples'}]
}]}

Note:

I have defined myValues as an empty list. I iterate over the file to read all lines and I create a new dict for each line, appending to myValues each time. Finally I create the whole data object with myValues embedded inside.

To get the above output I actually substituted: f = ['apples','another apples'] for the open file, but I'm sure an actual file would work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Ya objects inside a list does it. Helps a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.