I have a variable that looks like this:
data = {"add_content": {"errata_ids": [advisory]},"content_view_version_environments": [{"content_view_version_id": version_id}]}
I need to add single quotes to this variable , ie if I will assign the variables:
advisory
and version_id
and add the single quotes to data variable like this:
data = '{"add_content": {"errata_ids": ["RHSA-2017:1390"]},"content_view_version_environments": [{"content_view_version_id": 160}]}'
I am able to post to the API
I have tried to add the single quotes in variety of ways:
new_data = "'" + str(data) + "'"
>>> new_data
'\'{\'add_content\': {\'errata_ids\': [\'"RHSA-2017:1390"\']}, \'content_view_version_environments\': [{\'content_view_version_id\': \'160\'}]}\''
or using:
'"%s"'%(data)
and a few more ways.
How can I add the single quotes to the outer to the data variable before and after the opening {
and closing }
?
This is exactly what JSON does:
import json
new_data = json.dumps(data)
如果与之前的答案和注释相反,您没有尝试转换为json字符串,那么请在变量周围使用string.format:
data = {"add_content": {"errata_ids": '[{}]'.format(advisory)},"content_view_version_environments": [{"content_view_version_id": '{}'.format(version_id)}]}
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.