(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.
(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']}")
Output:
Manufacturer: VMware, Inc.
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
None
Again 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:
- open the file
- iterate over lines (3 in total) [first level of iteration]
- parse with literal_eval the single line, which results into a list.
- iterate over the list (from literal_eval) to get the list elements [second level of iteration]
- update the result dict, with the elements (dicts) from the list (result =| element)
- process the IPAddress to correct this entry
BTW: if you save the data in the right format, all these steps are not required. Look at the answer above,
(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.
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
None
Again 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
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'))
Output:
[[{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}],
[{'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'Index': '1'}],
[{'Name': 'DC01', 'UserName': 'None'}]]
VMware, Inc.
(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 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'))
Output:
[[{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}],
[{'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'Index': '1'}],
[{'Name': 'DC01', 'UserName': 'None'}]]
VMware, Inc.
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
Index starts at 0. Name is at 2
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
DC01
So 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