2015-09-04 94 views
1

我有這段代碼,但它說TypeError沒有足夠的格式字符串參數,即使我給它一個具有正確數量參數的元組。有人可以幫助我嗎?我看遍了堆棧溢出,python文檔似乎也沒有幫助。對於字符串不工作的Python百分比運算符

image_urls = { 
'horse.jpg': 'a.jpg', 
'bison.jpg': 'b.jpg', 
'bald_eagle.jpg': 'c.jpg', 
'pig_in_mud.jpg': 'd.jpg', 
'baby_monkey.jpg': 'e.jpg', 
'cute_golden_retriever.jpg': 'f.jpg', 
'cute_fluffy_bunny.jpg': 'g.jpg', 
'fluffy_kitten.jpg': 'h.jpg', 
'default_profile_pic.jpg': 'i.png' 
} 
mystr = '''<tr><td>Username:</td><td><input type='text' name='username'/></td></tr> 
     <tr><td>Profile picture:</td><td> 
      <table> 
       <tr><td><img src='%s'/></td><img src='%s'/><td></td><td><img src='%s'/></td></tr> 
       <tr><td><img src='%s'/></td><img src='%s'/><td></td><td><img src='%s'/></td></tr> 
       <tr><td><img src='%s'/></td><img src='%s'/><td></td><td><img src='%s'/></td></tr> 
      </table> 
     </td></tr> 
     <tr><td colspan="2" class="description"><i>Image must be 1MB or less.</i></td></tr> 
     <tr><td>Website:</td><td><input type='url' name='website' placeholder='example.com'/><td/></tr> 
     <tr><td colspan="2" class="description"><i>Got a home on the web? Put it here.</i></td></tr> 
     <tr><td>Bio:</td><td><textarea name='bio' placeholder='I am John Doe, and I enjoy doing this, that and a little more of this. (more content more elaboration)'></textarea></td></tr> 
     <tr><td colspan="2" class="description"><i>Tell about yourself here.</i></td></tr> 
     <tr><td>Gender:</td><td><input type='radio' name='gender'/> M <input type='radio' name='gender'/> F</td></tr> 
     <tr><td>Age:</td><td><input type='text' name='age'/></td></tr> 
     </table> 
     <h4>Privacy</h4> 
     <table> 
     <tr><td>Hide age</td><td><input type="checkbox" name="hide-age"/></td></tr> 
     <tr><td>Hide gender</td><td><input type="checkbox" name="hide-gender"/></td></tr> 
     <tr><td>Hide email</td><td><input type="checkbox" name="hide-email"/></td></tr> 
     </table> 
     <div class='button-jumbo'>Create account</div> 
     <script> 
     function onPasswordChange() { 
      var strength = this.value.length * 3; 
      var weakPasswords = ['password', '123456', '12345678', '1234', 'qwerty', '12345', 'dragon', 'baseball', 'football', 'letmein', 'security', 'monkey', '696969', 'abc123', '111111', 'ncc2701', 'trustno1']; 
      strength += (this.value.match(/[\/;'"\[\]\{\}\-_\[email protected]#\$\\\%\^\&\*\)\(]/g) ? (this.value.match(/[\/;'"\[\]\{\}\-_\[email protected]#\$\\\%\^\&\*\)\(]/g).length * 8): 0); 
      strength += (this.value.match(/[A-Z]/g) ? (this.value.match(/[A-Z]/g).length * 5) : 0); 
      strength += (this.value.match(/[0-9]/g) ? (this.value.match(/[0-9]/g).length * 6) : 0); 
      if (weakPasswords.indexOf(this.value.toLowerCase()) !== -1) { 
       strength = 5; 
      } 
      document.getElementById("password-meter").value = strength.toString(); 
     } 
     var pswdElt = document.getElementsByName('password')[0]; 
     pswdElt.oninput = onPasswordChange; 
     pswdElt.onchange = onPasswordChange; 
     pswdElt.onkeydown = onPasswordChange; 
     </script> 
     ''' % (image_urls['bald_eagle.jpg'], image_urls['pig_in_mud.jpg'], image_urls['cute_golden_retriever.jpg'], image_urls['bison.jpg'], image_urls['default_profile_pic.jpg'], image_urls['horse.jpg'], image_urls['baby_monkey.jpg'], image_urls['cute_fluffy_bunny.jpg'], image_urls['fluffy_kitten.jpg']) 
+0

你能否將這個例子縮小到最小? –

+0

您的正則表達式中有兩個額外的'%',需要在與格式化操作符一起使用的字符串中轉義('%%')。 – chepner

回答

1

你在你的正則表達式有%和Python認爲它應該把價值觀有太多

與%%正則表達式那你是怎麼逃脫它在Python

+0

謝謝,它現在正在工作! –

3

問題更換%在這一行:

strength += (this.value.match(/[\/;'"\[\]\{\}\-_\[email protected]#\$\\\%\^\&\*\)\(]/g) 

請注意,有一個%字符隱藏在那裏。 python字符串格式化程序着眼於此,試圖將其解釋爲(無效)格式說明符%\,並且因爲在元組中沒有它的匹配參數,將引發該異常。如果您在元組一個額外的參數,你會得到,而不是此異常:

ValueError: unsupported format character '\' (0x5c) at index 2017 

答案:逃跑的是,沒有被通過添加另一個%作爲格式說明任何%字符:

strength += (this.value.match(/[\/;'"\[\]\{\}\-_\[email protected]#\$\\\%%\^\&\*\)\(]/g) 

這將導致字符串格式化程序在輸出字符串中留下一個百分比。

+0

謝謝,這工作!我只是喜歡DorElias的回答,因爲它更短。 –