问题
I'm looking for a way to pass separately each ID from 'email' list as a string to my functions and retrieve 'customerId' and save it to a 'newList' for each item on email list.
my code:
email = ['733867', '7338704']
newList = []
for x in email:
def get_email_details(email, token, userId):
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'token': token,
'userId': userId}
endpoint = 'request/' + email + '/details'
return requests.get(url = HOST + endpoint, headers = headers, verify=False)
def get_customerId_list_per_email(email, token, userId):
for x in email:
r = json.loads(get_email_details(email, token, userId).text)
for c in r['newList']:
newList.append(c['customerId'])
return newList
customers = get_customerId_list_per_email(email, token, userId)
when I run the above I receive this error:
Traceback (most recent call last):
File "<stdin>", line 16, in <module>
File "<stdin>", line 13, in get_customerId_list_per_email
KeyError: 'newList'
Desired outcome when calling 'newList' should be:
>>> newList
[678975, 4356883]
It works for me, but only if I would submit a single email id:
#if trying with list like this: email = [733867970] then it won't work and will throw following error: TypeError: must be str, not list
email = ['733867970']
def get_email_details(email, token, userId):
headers = { 'accept': 'application/json',
'Content-Type': 'application/json',
'token': token,
'userId': userId}
endpoint = 'request/' + email + '/details'
return requests.get(url = HOST + endpoint, headers = headers, verify=False)
def get_customer_list_per_email(email, token, userId):
corrList = []
for x in email:
r = json.loads(get_email_details(email, token, userId).text)
for c in r['corrList']:
corrList.append(c['customerId'])
return corrList
customers = get_customer_list_per_email(email, token, userId)
When running the above I'm receiving single customerID for this emailID:
>>> corr
[733867972]
Could someone be able to help with this problem? Thanks in advance!
来源:https://stackoverflow.com/questions/61392355/run-functions-for-each-item-on-python-list-separately-and-append-results-retriev