是的,总之,我想知道为什么我会在我的钥匙和价值观面前看到你。
我正在渲染表格。表单具有特定标签的复选框和ip地址的一个文本字段。我正在创建一个字典,其中键是标签,在list_key中是硬编码的,字典的值是从表单输入(list_value)中获取的。字典已创建,但前面有一些值的u。这是字典的示例输出:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}}
有人可以解释一下我做错了什么。当我在pyscripter中模拟类似的方法时,我没有收到错误。欢迎任何改进代码的建议。谢谢
#!/usr/bin/env python
import webapp2
import itertools
import cgi
form ="""
<form method="post">
FIREWALL
<br><br>
<select name="profiles">
<option value="1">profile 1</option>
<option value="2">profile 2</option>
<option value="3">profile 3</option>
</select>
<br><br>
Check the box to implement the particular policy
<br><br>
<label> Allow Broadcast
<input type="checkbox" name="broadcast">
</label>
<br><br>
<label> Allow ARP
<input type="checkbox" name="arp">
</label><br><br>
<label> Allow Web traffic from external address to internal webserver
<input type="checkbox" name="webserver">
</label><br><br>
<label> Allow DNS
<input type="checkbox" name="dns">
</label><br><br>
<label> Block particular Internet Protocol address
<input type="text" name="ipaddr">
</label><br><br>
<input type="submit">
</form>
"""
dictionarymain={}
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write(form)
def post(self):
# get the parameters from the form
profile = self.request.get('profiles')
broadcast = self.request.get('broadcast')
arp = self.request.get('arp')
webserver = self.request.get('webserver')
dns =self.request.get('dns')
ipaddr = self.request.get('ipaddr')
# Create a dictionary for the above parameters
list_value =[ broadcast , arp , webserver , dns, ipaddr ]
list_key =['broadcast' , 'arp' , 'webserver' , 'dns' , 'ipaddr' ]
#self.response.headers['Content-Type'] ='text/plain'
#self.response.out.write(profile)
# map two list to a dictionary using itertools
adict = dict(zip(list_key,list_value))
self.response.headers['Content-Type'] ='text/plain'
self.response.out.write(adict)
if profile not in dictionarymain:
dictionarymain[profile]= {}
dictionarymain[profile]= adict
#self.response.headers['Content-Type'] ='text/plain'
#self.response.out.write(dictionarymain)
def escape_html(s):
return cgi.escape(s, quote =True)
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
答案 0 :(得分:149)
字符串值前面的“u”表示该字符串已表示为unicode。字符串前面的字母称为“字符串编码声明”。 Unicode是一种表示比ascii可以管理的更多字符的方式。
您可以通过多种方式将字符串转换为unicode:
>>> u'foo'
u'foo'
>>> unicode('foo')
u'foo'
但真正的原因是代表这样的事情(translation here):
>>> val = u'Ознакомьтесь с документацией'
>>> val
u'\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439'
>>> print val
Ознакомьтесь с документацией
在大多数情况下,在此代码中处理它们时不应该出现与ascii字符串不同的错误。
您将看到其他符号,例如用于告诉字符串不解释任何特殊字符的“原始”符号。在python中进行正则表达式时,这非常有用。
>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'
ASCII和Unicode字符串在逻辑上是等价的:
>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True
答案 1 :(得分:9)
这是一个功能,而不是错误。
请参阅http://docs.python.org/howto/unicode.html,特别是'unicode type'部分。