-5

I want to use the following text, and parse it to python, but can't find a good way for that the content is in a text file, and I want to use the data in python. what kind of type to use? I want to populate it to a menu.

"us-west-2": {
  "focal-amd64": "ami-06e54d05255faf8f6",
  "rhel7.5-x86_64": "ami-6f68cf0f",
  "xenial-amd64": "ami-09b42c38b449cfa59",
  "bionic-amd64": "ami-03804ed633fe58109",
  "rhel8-x86_64": "ami-02f147dfb8be58a10"
},
"us-east-1": {
  "oracle6.5": "ami-c034c5ad",
  "rhel7.5-x86_64": "ami-0394fe9914b475c53",
  "rhel7.7-x86_64": "ami-0916c408cb02e310b",
  "trusty-amd64": "ami-0d859172c8670bbcd",
  "xenial-amd64": "ami-028d6461780695a43",
  "rhel7.6-x86_64": "ami-08a7d2bfef687328f",
  "aws-ena": "ami-ccd280db",
  "rhel8-x86_64": "ami-098f16afa9edf40be",
  "rhel7.9-x86_64": "ami-005b7876121b7244d",
  "bionic-amd64": "ami-07025b83b4379007e",
  "centos6.9-x86_64": "ami-25491433",
  "focal-amd64": "ami-0dba2cb6798deb6d8"
},
"us-east-2": {
  "focal-amd64": "ami-07efac79022b86107",
  "rhel7.9-x86_64": "ami-0d2bf41df19c4aac7",
},
"eu-central-1": {
  "bionic-amd64": "ami-054e21e355db24124",
  "xenial-amd64": "ami-05710338b6a5013d1",
},
"eu-west-1": {
  "focal-amd64": "ami-06fd8a495a537da8b",
  "rhel7.5-x86_64": "ami-02ace471",
  "rhel7.6-x86_64": "ami-04c89a19fea29f1f0",
  "rhel7.9-x86_64": "ami-020e14de09d1866b4",
  "bionic-amd64": "ami-0727f3c2d4b0226d5",
  "rhel8-x86_64": "ami-08f4717d06813bf00"
},
"ap-northeast-1": {
  "bionic-amd64": "ami-003371bfa26192744",
  "xenial-amd64": "ami-097beac0bacfefe65",
  "focal-amd64": "ami-09b86f9709b3c33d4",
}
Yoni
  • 230
  • 2
  • 5

1 Answers1

2

Your data is almost in json format: if you add enclosing brackets {} around it, and remove any commas , before a closing bracket (there are 3 of them), your data will be fully json-compliant.

Now assuming your data is in a file called foobar.json, the following code will parse it into a python dictionary named d:

import json

with open('foobar.json', 'r') as f:
    d = json.load(f)

You can now easily access and use your data, for example

print(d['us-west-2']['focal-amd64'])

will print ami-06e54d05255faf8f6.

joao
  • 2,220
  • 2
  • 11
  • 15