简体   繁体   中英

Loop List Python for Variable

Im trying to loop through a list and insert the 'extensionid' into the URL

extensionid = ['1234','12356']
url = '/restapi/v1.0/account/~/extension/'+extensionid+'/presence'

params = {

    'dndStatus': "TakeAllCalls",

}

resp = platform.get(url, params,)
print ((resp.text()))

But I get the error

url = '/restapi/v1.0/account/~/extension/'+extensionid+'/presence' TypeError: can only concatenate str (not "list") to str [Finished in 1.121s

What am I doing wrong?

Thanks!

You probably need.

extensionid = ['1234','12356']
url = '/restapi/v1.0/account/~/extension/{}/presence'

params = {
    'dndStatus': "TakeAllCalls",
}

for id in extensionid:     #iterate each id
    resp = platform.get(url.format(id), params)  #form URL and query. 
    print ((resp.text()))

extensionid is a list of strings. So, you cannot concatenate a list with a string as error is telling you.

To access a specific item of the list you have to use an index.

For example:

extensionid[0] // it contains 1234
extensionid[1] // it contains 12356

So, your url can be written like this:

url = '/restapi/v1.0/account/~/extension/'+extensionid[0]+'/presence'

In this case it will be evaluated by python as:

url = '/restapi/v1.0/account/~/extension/1234/presence'

Please consider this simple documentation about lists:

https://www.programiz.com/python-programming/list

To iterate the elements of a list you can use:

for element in extensionid:
    url='/restapi/v1.0/account/~/extension/'+ element +'/presence'
    print(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM