Python Forum
How can I organize my code according to output that I want
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I organize my code according to output that I want
#1
Hi everyone. I have the code below.

import json

with open('data6.json') as f:
    data = json.load(f)
    for item in data:
        for dct in item:
            print('------------------')
            for key in ['Manufacturer', 'Name']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")
            for key in ['IPAddress']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")
            for key in ['UserName']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")


My output is like:
------------------
Manufacturer --> VMware, Inc.
Name --> DC01
IPAddress --> Key Not Present
UserName --> Key Not Present
------------------
Manufacturer --> Key Not Present
Name --> Key Not Present
IPAddress --> ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
UserName --> Key Not Present
------------------
Manufacturer --> Key Not Present
Name --> DC01
IPAddress --> Key Not Present
UserName --> None
But i want an Output like:


Manufacturer --> VMware, Inc.
Name --> DC01
IPAddress --> ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
UserName --> DC01
How should I organize my code for such an output?
Reply
#2
(Mar-11-2022, 08:24 AM)ilknurg Wrote: How should I organize my code for such an output?

We have no idea what is the structure of data6.json file so how could we know?

Regardless of that - why use three for loops?

for dct in item:
            print('------------------')
            for key in ['Manufacturer', 'Name']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")
            for key in ['IPAddress']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")
            for key in ['UserName']:
                print(f"{key} --> {dct.get(key, 'Key Not Present')}")

# same functionality:

for dict_ in record:
    for key in ['Manufacturer', 'Name', 'IPAddress', 'UserName']:
        print(f"{key} --> {dict_.get(key, 'Key Not Present')}")
   
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 388 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  How do I organize my simple ftp modules? blobdx7 3 537 Jan-05-2024, 01:23 PM
Last Post: Gribouillis
  I cannot able to see output of this code ted 1 762 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,138 Jun-23-2022, 09:18 PM
Last Post: William369
Star Split and organize my Pandas Dataframe brunolelli 4 2,723 Apr-18-2021, 03:00 AM
Last Post: brunolelli
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,684 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Why this code not getting desired output ? MDRI 2 2,544 Sep-18-2020, 02:11 AM
Last Post: MDRI
  Best way(s) to organize and use "global" values? pjfarley3 6 2,660 Sep-03-2020, 12:27 AM
Last Post: pjfarley3
  I couldn't understand the output of the below code ravich129 1 1,936 Dec-12-2019, 06:24 AM
Last Post: sandeep_ganga
  Output of Python code hemal07yc 5 3,977 Sep-13-2019, 11:33 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020