Python字符串替换子字符串(求值子字符串dict)

Python字符串替换子字符串(求值子字符串dict),python,string,string-formatting,Python,String,String Formatting,嗨,我需要进行API调用来收集一些数据。URL如下所示: https://app.example.com/SearchService/search/xref?parts=[{"partNumber":"myproduct","manufacturer":"my company"}]&fmt=json 我尝试用不同产品的列表替换partnumber值,我正在考虑使用子字符串替换 >>> print "My name is {0}".format('/usr/bin')

嗨,我需要进行API调用来收集一些数据。URL如下所示:

https://app.example.com/SearchService/search/xref?parts=[{"partNumber":"myproduct","manufacturer":"my company"}]&fmt=json
我尝试用不同产品的列表替换partnumber值,我正在考虑使用子字符串替换

>>> print "My name is {0}".format('/usr/bin')
My name is /usr/bin
但是,当我试图针对URL执行此操作时:

>>> print 'https://app.example.com/SearchService/search/xref?parts=[{"partNumber":"{0}","manufacturer":"my company"}]&fmt=json'.format('my product')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: '"partNumber"'
>>打印'https://app.example.com/SearchService/search/xref?parts=[{“零件号”:“{0}”,“制造商”:“我的公司”}]&fmt=json.format('我的产品')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
KeyError:““零件号””
不知何故,它试图在字符串中计算字典,这完全超出了我的知识范围,它为什么这样做


谁能帮我修一下吗

您需要使用
{{
}
来转义另一个
{
}

>>> print 'https://app.example.com/SearchService/search/xref?parts=[{{"partNumber":"{0}","manufacturer":"my company"}}]&fmt=json'.format('my product')
https://app.example.com/SearchService/search/xref?parts=[{"partNumber":"my product","manufacturer":"my company"}]&fmt=json
发件人:

如果需要在文本中包含大括号字符,可以 可以通过加倍来转义:{{和}