Я ищу способ передачи каждого идентификатора из списка «email» в виде строки в мои функции, извлечения «customerId» и сохранения его в «newList» для каждого элемента в списке электронной почты.
мой код:
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)
, когда я запускаю вышеупомянутое, я получаю эту ошибку:
Traceback (most recent call last):
File "<stdin>", line 16, in <module>
File "<stdin>", line 13, in get_customerId_list_per_email
KeyError: 'newList'
Желаемый результат при вызове 'newList' должен быть:
>>> newList
[678975, 4356883]
Это работает для меня, но только если я отправлю один идентификатор электронной почты:
#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)
При выполнении вышеизложенного я получаю один идентификатор клиента для этого emailID:
>>> corr
[733867972]
Может ли кто-нибудь помочь с этой проблемой? Заранее спасибо!