Mar-10-2022, 12:35 PM
How can i parse my output?
How can i parse my output?
|
Mar-10-2022, 12:39 PM
(Mar-10-2022, 12:29 PM)menator01 Wrote:data = {'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01', 'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'UserName': None} print(f"Manufacturer: {data['Manufacturer']}") I dont understand why it doesnt work on my pc. I gave an error. Traceback (most recent call last): File "C:\Users\DELL\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_38.py", line 4, in <module> print(f"Manufacturer: {data['Manufacturer']}") TypeError: list indices must be integers or slices, not str (Mar-10-2022, 11:09 AM)ilknurg Wrote: For parsing it, im trying to write this content in a JSON file and i tried read and parse it.It's already a data structure so making it JSON and read just a extra unnecessary step. Just a copy of your data and make into a lst variable.lst = [ [{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}], [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}], [{"Name": "DC01", "UserName": None}], ] >>> for i in lst: ... print(i[0].get('Name')) ... DC01 None DC01 >>> for i in lst: ... print(i[0].get("Manufacturer")) ... VMware, Inc. None NoneAgain just a copy of your data and the extra JSON step. import json with open('data.json', 'r') as myfile: data = json.load(myfile) >>> for i in data: ... print(i[0].get('Name')) ... DC01 None DC01 >>> for i in data: ... print(i[0].get("Manufacturer")) ... VMware, Inc. None None
Because your data is still a list. You need to get the content from the list and this is your dict.
The for-loop ist nested with a second for-loop. In my example, I do the following:
BTW: if you save the data in the right format, all these steps are not required. Look at the answer above,
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Mar-10-2022, 01:07 PM
(Mar-10-2022, 12:41 PM)snippsat Wrote:(Mar-10-2022, 11:09 AM)ilknurg Wrote: For parsing it, im trying to write this content in a JSON file and i tried read and parse it.It's already a data structure so making it JSON and read just a extra unnecessary step. Thank you. The code will work with the json you wrote. I want to ask one more thing. My json is coming me like : ['[{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}]', '[{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}]', '[{"Name": "DC01", "UserName": null}]']How can i reorganize it for working the code you gave me? (Mar-10-2022, 01:07 PM)ilknurg Wrote: My json is coming me like : lst = [ [{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}], [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}], [{"Name": "DC01", "UserName": None}], ]So your data that i just copy before,then without lst copy into JSON Formatter.It will fix a couple of thing,then use data.json .import json from pprint import pprint with open('data.json', 'r') as myfile: data = json.load(myfile) pprint(data) print(data[0][0].get('Manufacturer'))
Mar-10-2022, 01:51 PM
(Mar-10-2022, 01:43 PM)snippsat Wrote:(Mar-10-2022, 01:07 PM)ilknurg Wrote: My json is coming me like :lst = [ [{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}], [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}], [{"Name": "DC01", "UserName": None}], ]So your data that i just copy before,then without Thank you. It helped me a lot. But i didnt understand the meaning of data[0][0]. It is the first element of data array? I try to reach Name, I wrote data[0][3] and i get IndexError: list index out of range
Mar-10-2022, 02:08 PM
Index starts at 0. Name is at 2
I welcome all feedback.
The only dumb question, is one that doesn't get asked. My Github How to post code using bbtags
The structure is list of list with dictionary inside.
>>> data[0] [{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}] >>> data[0][0] {'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'} >>> type(data[0][0]) <class 'dict'> >>> >>> data[0][0]['Name'] 'DC01' # Or >>> data[0][0].get('Name') 'DC01'In a loop as i showed before can do it like this. >>> for item in data: ... print(item[0].get('Name', 'Not found in list')) ... DC01 Not found in list DC01So using dictionary .get() it dos not stoop loop with error,and get both Name.>>> for item in data: ... print(item[0]['Name']) ... DC01 Traceback (most recent call last): File "<interactive input>", line 2, in <module> KeyError: 'Name' # Fix >>> for item in data: ... try: ... print(item[0]['Name']) ... except KeyError: ... pass ... DC01 DC01
Mar-10-2022, 02:17 PM
(This post was last modified: Mar-10-2022, 02:17 PM by Gribouillis.)
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
How can i parse my output? | ilknurg | 4 | 1,623 |
Mar-16-2022, 03:27 PM Last Post: snippsat |
|
Unable to parse JSON output | dragan979 | 1 | 3,583 |
Apr-20-2018, 02:24 PM Last Post: dragan979 |
Users browsing this thread: 1 Guest(s)