尝试使用Python中的字符串格式替换Python字符串中的值时出错

尝试使用Python中的字符串格式替换Python字符串中的值时出错,python,Python,我有这根绳子 S="191042709832779540946_1513246254239&source=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22from%22%3A%s%2C%22size%22%3A20000%2C%22facets%22%3A%7B%22_type%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22_type%22%2C%22size%22%3A102%2C%22or

我有这根绳子

S="191042709832779540946_1513246254239&source=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22from%22%3A%s%2C%22size%22%3A20000%2C%22facets%22%3A%7B%22_type%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22_type%22%2C%22size%22%3A102%2C%22order%22%3A%22reverse_term%22%7D%7D%2C%22index.classification.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.classification.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.has_seal.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.has_seal.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.license.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.license.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.publisher.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.publisher.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.language.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.language.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%7D%7D&_=1513246254241"
在上面的字符串中,我必须在某个地方替换一个整数。为此,我使用了%s

s = S % 100
然后我得到了以下错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'B' (0x42) at index 51
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:索引51处的格式字符“B”(0x42)不受支持
注意:您可以搜索(ctrl+f)以在上面的字符串中查看%s。我试着突出它。但这是不可能的


如何替换上述字符串中的值。提前感谢。

您只需使用替换即可

newS = S.replace("%s", "100")

在这里,
%
的常用语法很难应用,因为在字符串中有许多“%x”python没有按字面意思理解(它认为它们是其他替换的占位符,但在CAE中只有转义的HTML字符)

您需要避免使用
%s
进行替换,并将
{}
与Python的
format()
函数一起使用,如下所示:

text = "191042709832779540946_1513246254239&source=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22from%22%3A{}%2C%22size%22%3A20000%2C%22facets%22%3A%7B%22_type%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22_type%22%2C%22size%22%3A102%2C%22order%22%3A%22reverse_term%22%7D%7D%2C%22index.classification.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.classification.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.has_seal.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.has_seal.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.license.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.license.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.publisher.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.publisher.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.language.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.language.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%7D%7D&_=1513246254241"
replace = 100

print text.format(replace)
这将给你:

191042709832779540946_1513246254239&source=%7B%22query%22%3A%7B%22match_all%22%3A%7B%7D%7D%2C%22from%22%3A100%2C%22size%22%3A20000%2C%22facets%22%3A%7B%22_type%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22_type%22%2C%22size%22%3A102%2C%22order%22%3A%22reverse_term%22%7D%7D%2C%22index.classification.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.classification.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.has_seal.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.has_seal.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.license.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.license.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.publisher.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.publisher.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%2C%22index.language.exact%22%3A%7B%22terms%22%3A%7B%22field%22%3A%22index.language.exact%22%2C%22size%22%3A110%2C%22order%22%3A%22count%22%7D%7D%7D%7D&_=1513246254241

这是必需的,因为您的
文本
已经有其他
%
字符,并且这将与
%s
替换不兼容。通过使用此方法,
replace
如果需要,也可以是字符串。

在某个地方替换整数,在哪里替换?你能更好地解释一下吗?向我们展示代码。作为旁注,
replace
可以是任何东西,因此OP可以直接使用数字,而不必首先将其转换为字符串(带有
“XXX”的示例)
在这方面可能会令人困惑)。