I have python dictionary class that is generated from API call returning below result
{'url': ' https://propertypro.zendesk.com/api/v2/tickets/4249.json ', 'id': 4249, 'external_id': None}
{'url': ' https://propertypro.zendesk.com/api/v2/tickets/4089.json ', 'id': 4089, 'external_id': None}
the code as follow;
from urllib.parse import urlencode
import requests
credentials = 'some email', 'some password'
session = requests.Session()
session.auth = credentials
params = {
'query': 'type:ticket tags:test_python',
'sort_order': 'asc'
}
url = 'https://propertypro.zendesk.com/api/v2/search.json?' + urlencode(params)
response = session.get(url)
if response.status_code != 200:
print('Status:', response.status_code,
'Problem with the request. Exiting.')
exit()
# Print the subject of each ticket in the results
data = response.json()
I iterate the data in order to get all values from key 'id' to be assigned to variable id_merged
as String with comma separated
for result in data['results']:
# Ticket to update
id_merged = (result['id'])
but get only one result instead of 2, the var gets overwritten in the loop?
from test_search import data
import json
import requests
# Iterate the search result
for result in data['results']:
# Ticket to update
id_merged = (result['id'])
print("**Start**")
print(id)
body = 'Test ticket'
# Package the data in a dictionary matching the expected JSON
data_comment = {'ticket': {'comment': {'body': body}}}
# Encode the data to create a JSON payload
payload = json.dumps(data_comment)
# Set the request parameters
url = 'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' + \
'ids=' + str(id_merged)
user = 'some email'
pwd = 'some password'
headers = {'content-type': 'application/json'}
# Do the HTTP put request
response = requests.put(url, data=payload,
auth=(user, pwd), headers=headers)
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code,
'Problem with the request. Exiting.')
exit()
# Report success
print('Successfully added comment to ticket #{}'.format(id))
I'd like to get a result like 'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' + \\ 'ids=' + 4249,4089
'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' + \\ 'ids=' + 4249,4089
.
How do I get this?
If you want to turn the values of id
into a comma delimited string, you can do the following.
','.join(id.values())
So change your code to
url = 'str,' + 'str1:' + ','.join(id.values())
For completeness, if you wanted to do the same thing with the dictionary keys, you'd just use
','.join(id)
Looks like you are getting list of list in dict values, in that case you can flatten the list and join on string "," like:
>>> import itertools
>>> ",".join(map(str, list(itertools.chain(*[[1,2,3],[4,5]]))))
'1,2,3,4,5'
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.