Python Forum
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)

Pages: 1 2 3


How can i parse my output? - ilknurg - Mar-10-2022

I have this code.

import json

with open('data5.json', 'r') as myfile:
    data = myfile.read()
    data_2 = data.replace('[', ' ')
    data_3 = data_2.replace(']', ' ')
    print(data_3)
My variable data_3 is like that:


  {"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" } 
I want to parse this. I want to get the value of Manufacturer or Model or Name. How can i do it?


RE: How can i parse my output? - Gribouillis - Mar-10-2022

Why do you import json if you don't use json.load() or loads() ? Module json is the json parser included in python.


RE: How can i parse my output? - ilknurg - Mar-10-2022

ı tried to use json load

import json
with open('data6.json', 'r') as myfile:
    data = json.load(myfile)
and data6.json is like that

{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }
but it gives the error

Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_38.py", line 3, in <module>
    data = json.load(myfile)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 72 (char 71)

Process finished with exit code 1



RE: How can i parse my output? - DeaD_EyE - Mar-10-2022

Sure, that the square-brackets are missing in the JSON file? If yes, then your file is malformed and could not parse without modification. The JSON module of the Python standard lib is very strict.

If this is the content of your input file:
{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }
Then you may add [ and ] to the str.
# first you just read your json file
# here I just put the raw str into the code, because of laziness...
data_as_text = """{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"} ,  {"Index": "1", "IPAddress":  "192.168.1.240,fe80::350e:d28d:14a5:5cbb" }"""

# parsing
my_data = json.loads("[" + data_as_text + "]")
But it's better to have valid JSON content.

Sometimes people add line by line JSON data.
This is also not parsable with json because it's not valid.


RE: How can i parse my output? - Gribouillis - Mar-10-2022

The parser fails because data6.json is not valid JSON syntax. Where do you get this file from ? I suggest to try with data5.json instead.


RE: How can i parse my output? - ilknurg - Mar-10-2022

I get your suggessions. I have 3 vmi commands and there are the result of its.

[{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}]
[{'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']}] 
[{'Name': 'DC01', 'UserName': None}]
They are seperate strings. I create an output_array and appent all these 3 lines into it.
So the output_array is :

[[{'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 parsing it, im trying to write this content in a json file and i tried read and parse it.
When i use json validator it says that it is a valid json.
Now i tried to run this code

import json
with open('data5.json', 'r') as myfile:
    data = json.load(myfile)
    for i in data:
        print(data['Name'])
And again getting the error

Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_38.py", line 5, in <module>
    print(data['Name'])
TypeError: list indices must be integers or slices, not str
I want to reach this values
Name
Manucaturer
Username.


i'm stuck here


RE: How can i parse my output? - DeaD_EyE - Mar-10-2022

Hm... what if all elements of the 3 lists are packed into one dict?

(Mar-10-2022, 11:09 AM)ilknurg Wrote: [{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01'}]
[{'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']}]
[{'Name': 'DC01', 'UserName': None}]

The single quotes are invalid for use in JSON.
I look more like Python Code.
You could use ast.literal_eval, which is safe.


from ipaddress import ip_address
from ast import literal_eval


def parse(file):
    result = {}

    with open("data.txt") as fd:
        for line in fd:
            # skipping empty lines with only
            # whitespace
            if not line.strip():
                continue

            for element in literal_eval(line):
                result |= element

    # example to change existing data on result
    # list contains 1 str with many ip addresses
    # the addresses are splitted by a comma
    if "IPAddress" in result:
        ip_addresses = []

        # get the list from IPAddresses
        # and take the first element (there is only one)
        # then split this str by comma
        for ip in result["IPAddress"][0].split(","):
            # creating a IPv4Address or IPv6Address instance from
            # the str of the ip address. Not required
            ip_addresses.append(ip_address(ip))
            # to append only the str
            # ip_address.append(ip)

        # assign the ip_addresses
        result["IPAddress"] = ip_addresses

    return result



RE: How can i parse my output? - ilknurg - Mar-10-2022

Thank you for your answer.
In my database, i have a column which name is Manufacturer.

I want to get Manufacturer info from this line, and save it to in my db.


{'Manufacturer': 'VMware, Inc.', 'Model': 'VMware7,1', 'Name': 'DC01', 'Index': '1', 'IPAddress': ['192.168.1.240,fe80::350e:d28d:14a5:5cbb'], 'UserName': None}
How can i extract only the manufacturer? I want to parse it


RE: How can i parse my output? - DeaD_EyE - Mar-10-2022

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.


RE: How can i parse my output? - menator01 - Mar-10-2022

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.