2017-12-14 4 views
1

に文字列フォーマットを使用して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%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 

注:あなたは上記の文字列でそれを見るために%sの(Ctrl + Fキー)を検索することができます。私はそれを強調しようとした。しかし、それは不可能でした。

上記の文字列の値をどのように置き換えることができますか。前もって感謝します。

あなたは、単にそれは彼らが考えているので、あなたの文字列に「%x」はの多くは(Pythonは文字通り取らないことがある

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

ここ%と通常の構文は適用することは困難であるを交換使用することができます

+1

"ある場所で整数を代入する"の代わりに_where_を使用しますか? – Rishav

+0

あなたはより良く説明できますか?、私たちにコードを教えてください。 – exe

答えて

0

他のいくつかの代替のためのプレースホルダが、あなたのCAESにそこはちょうどHTML文字をエスケープしている)

2

あなたが置換のため%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) 

これはあなたを与えるだろう:あなたのtextはすでに他の%の文字を持っており、これは%s置換とは互換性がありませんよう

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 

、これが必要です。この方法を使用すると、必要に応じてreplaceを文字列にすることもできます。

+0

補足として、 'replace'は何でもかまいません。そのためOPは文字列を最初に変換することなく番号を直接使用できます(その点では" XXX "の例が混乱するかもしれません)。 – GPhilo