我有这个字符串
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"
在上面的字符串中,我必须在某个地方替换Integer。为此,我使用%s。
s = S % 100
然后我收到以下错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'B' (0x42) at index 51
注意:您可以搜索(ctrl + f)%s以在上面的字符串中查看它。我试图突出它。但这是不可能的。
如何替换上述字符串中的值。提前谢谢。
答案 0 :(得分:3)
您需要避免使用%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
这是必要的,因为您的text
已有其他%
个字符,但这与%s
替换不兼容。通过使用此方法,如果需要,replace
也可以是字符串。
答案 1 :(得分:0)
您只需使用替换
即可newS = S.replace("%s", "100")
这里使用%
的常用语法很难应用,因为在你的字符串中有很多&#39;%x&#39; python不是字面意思(它认为它们是其他替代品的占位符,但在你的caes中只有转义的HTML字符)