How do I iterate through two lists in API call Python 3

651 Views Asked by At

I have two files containing information that I need to input in the same script. One containing ID's, one on each line, and the other list containing parameters on their own individual lines as well. It should be made known that this list contains over 4000 lines each. Other API calls have been successful but this one is a bit harder to figure out.

The way this is intended to work is that the script reads line 1 from the ID file, insert that ID where %s is in the url. This will complete the url necessary for the API call. Then I need the parameters which are on the same lines matching their respective network ID's in the ID file, placed in %s in the payload section.

I got it to this point and what is happening now is when an ID is picked in the ID list, the URL becomes complete and does what it is supposed to. However when the script starts reading the contents file, it is iterating over and over until all parameters for ALL networks are complete and this is applied for just that one network, which is not supposed to happen, then it moves onto the next network ID and does the same thing.

I posted a sample visual to give you an idea of what the output is. I know there must be a way to have them read one line at a time, run the script, and iterate over to the next line in sequence, and do this until both of the entire lists are complete.

Python is not my strongest area so any assistance is greatly appreciated. The files are .txt files and properly formatted. These data have been tested using postman and have been successful in our other API calls as well, so we can eliminate a couple of factors.

with open('TZ.txt') as file1, open ('TZContents.txt') as file2:
 array1 = file1.readlines()
 file = file2.readlines()
 for line in array1:
         url = 'https://dashboard.meraki.com/api/v0/networks/%s' %line.rstrip("\n")
         for line2 in file:
             payload = '%s' % line2.rstrip("\n")
             headers = {'X-Cisco-Meraki-API-Key': 'API Key','Content-Type': "application/json"}
             response = requests.request('PUT', url, headers = headers, data = payload, allow_redirects=True, timeout = 10)
             print(response.text)

Output Example Below:

{"id":"1111", "type":"wireless","name":"Network A}
{"id":"1111", "type":"wireless","name":"Network B}
{"id":"1111", "type":"wireless","name":"Network C}
{"errors":["Name has already been taken"]}
{"errors":["Name has already been taken"]}
{"errors":["Name has already been taken"]}
{"errors":["Name has already been taken"]}
{"errors":["Name has already been taken"]}
{"id":"2222", "type":"appliance","name":"Network A}
{"id":"2222", "type":"appliance","name":"Network B}
{"id":"2222", "type":"appliance","name":"Network C}

Should be this:

{"id":"1111", "type":"wireless","name":"Network A} 
{"id":"2222", "type":"appliance","name":"Network B}
{"id":"3333", "type":"combined","name":"Network C}
1

There are 1 best solutions below

3
meizhu812 On

I read your description and I guess that the two files contain exactly the same number of lines. Is that correct?

In the present code a nested for iterations is used, resulting in redudant output. You might use a same index to locate the same line in either file.

A modified code might be

with open('TZ.txt') as file1, open ('TZContents.txt') as file2:
    ids = file1.readlines()
    params = file2.readlines()
    n_lines = len(ids)
    for line_num in list(range(n_lines)):
        url = 'https://dashboard.meraki.com/api/v0/networks/%s' %ids[line_num].rstrip("\n")
        payload = '%s' % params[line_num].rstrip("\n")
        headers = {'X-Cisco-Meraki-API-Key': 'API Key','Content-Type': "application/json"}
        response = requests.request('PUT', url, headers = headers, data = payload, allow_redirects=True, timeout = 10)
        print(response.text)