对于字符串不工作的Python百分比运算符

问题描述:

我有这段代码,但它说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

你在你的正则表达式有%和Python认为它应该把价值观有太多

与%%正则表达式那你是怎么逃脱它在Python

+0

谢谢,它现在正在工作! –

问题更换%在这一行:

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的回答,因为它更短。 –