0

I am trying to get every occurrence found in a JSON output that is in my dictionary to append to a list but every time it finds another occurrence it makes a new list with the new one added. I believe this may have something to do with my for loop, but I am not sure.

My code:

def get_all_asset_groups():

    path1 = "/api/3/asset_groups/?size=300"
    url5 = server + path1
    response = requests.request("GET", url5, headers=headers, verify=False)
    data = response.json()
    x = (json.dumps(data, indent=2, sort_keys=True))

    server_owners_dict=dict()
    owners= []

    #populating the dictionary with the server owners
    for i in data['resources']:
            if 'id' in i and 'name' in i:
                b = i['name'], i['id']
                k = str(b)
                if  re.findall("'Server-Ownership-\w+', \w+", k):
                    matchedOwners = re.findall("'Server-Ownership-\w+', \w+", k)[0]
                    j = str(matchedOwners).strip().split() 
                    server_owners_dict[j[0].replace('Server-Ownership-','')]=j[1]               

    with open(PATH, mode='w') as my_csv_file:
        employee_writer = csv.writer(my_csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        employee_writer.writerow(['Server Name','Criticals'])
        for owner in server_owners_dict:
            url = server + "/api/3/asset_groups/" + str(server_owners_dict[owner]) + "/assets"
            response = requests.request("GET", url, headers=headers, verify=False, params = {owner:server_owners_dict[owner]})
            data = response.json()
            for i in data['resources']:
                url = server+ "/api/3/assets/" + str(i)
                response = requests.request("GET", url, headers=headers, verify=False)
                data = response.json()
                if data['vulnerabilities']['critical'] > 0:
                    d = owner                    
                    owners.append(owner)
                    print(owners)

Output:

["'Bob',"]
["'Bob',", "'Bob',"]
["'Bob',", "'Bob',", "'Bob',"]

What i would like for it to be:

["'Bob',","'Bob',","'Bob',"]

Also, is there any way i can get rid of these extra characters to have just:

["Bob", "Bob", "Bob"]
pythonscrub
  • 75
  • 1
  • 7
  • Did you try this? https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list – CCebrian Feb 24 '20 at 16:13
  • @CCebrian I can make a csv file fine, but i need to get this output right so that i can count the occurrences and have just the name and the number of total occurrences for each owner – pythonscrub Feb 24 '20 at 16:15
  • It is not the `append` that is causing the repeated print, it is the `print` itself that is getting called every time in the loop. Dedent your `print` statement appropriately. – Sayandip Dutta Feb 24 '20 at 16:15
  • @SayandipDutta how can i verify that the list was created properly then? i tried moving it out and it still causes issues – pythonscrub Feb 24 '20 at 16:16
  • I was suggesting to dedent the print statement, not remove it. When it finally prints, you will get to see whether it was created properly. – Sayandip Dutta Feb 24 '20 at 16:17
  • align `print(owners)` with the `for owner...` loop call. This way you will only print the list once. – RogB Feb 24 '20 at 16:18
  • trying that right now, it may take a little bit to run as there is a lot of data. are you aware of a way to count the occurrences for each owner? i tried counter but it counter individual characters – pythonscrub Feb 24 '20 at 16:22
  • also when i dedent to the for loop it now gives me a key error where data['resources'], but i know this key works and pulls the proper info – pythonscrub Feb 24 '20 at 16:24
  • @pythonscrub,you are printing the list inside the ~if block~,you should print the list outside the `if block`... – Aayman Khalid Feb 24 '20 at 16:44
  • @pythonscrub,also what is the purpose of this d? you can also do this owners.append(d) – Aayman Khalid Feb 24 '20 at 16:46
  • it is not needed for this, i thought i did. is there any way for me to remove the extra characters i am getting in my output? – pythonscrub Feb 24 '20 at 16:48
  • @pythonscrub,plus if you haven't noticed,there are two double quotes in the list(every element),maybe you should check the way input is being given. – Aayman Khalid Feb 24 '20 at 16:49
  • i cannot figure out why it is doing this. i figured i could just write something to manually remove them – pythonscrub Feb 24 '20 at 16:51
  • @pythonscrub, I think you should check the input,where that owner part is coming from why does it have double quotes and a comma? – Aayman Khalid Feb 24 '20 at 17:00

0 Answers0