How can i parse my output? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can i parse my output? (/thread-36613.html) |
RE: How can i parse my output? - ilknurg - Mar-10-2022 (Mar-10-2022, 12:24 PM)DeaD_EyE Wrote: I changed my code to read a file, and before I didn't see that the list with the IPAddress does contain only one element. The first element contains all addresses separated by a comma. Thank you I will looked at the code. RE: How can i parse my output? - ilknurg - Mar-10-2022 (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 RE: How can i parse my output? - snippsat - Mar-10-2022 (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 RE: How can i parse my output? - DeaD_EyE - Mar-10-2022 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, RE: How can i parse my output? - ilknurg - Mar-10-2022 (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? RE: How can i parse my output? - snippsat - Mar-10-2022 (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'))
RE: How can i parse my output? - ilknurg - Mar-10-2022 (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 RE: How can i parse my output? - menator01 - Mar-10-2022 Index starts at 0. Name is at 2 RE: How can i parse my output? - snippsat - Mar-10-2022 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 RE: How can i parse my output? - Gribouillis - Mar-10-2022 The Python Tutorial |