是的,简而言之,我想知道为什么我会在我的键和值前面看到一个u。
我正在呈现一个表单。该表单具有特定标签的复选框和ip地址的文本字段。我正在创建一个字典,它的键是标签,硬编码在list_key中,字典的值取自表单输入(list_value)。创建了字典,但是字典前面有一些值。以下是字典的示例输出:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}}
有人能解释一下我做错了什么吗?我没有得到错误,当我模拟类似的方法在脚本中。欢迎任何改进代码的建议。谢谢
#!/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)
发布于 2012-07-01 11:52:23
字符串值前面的'u‘表示字符串是Unicode字符串。Unicode是一种表示比普通ASCII可处理的字符更多的字符的方法。事实上,您看到的u
意味着您使用的是Python2-在Python3中,字符串默认是Unicode,但是在python2中,前面的u
可以区分Unicode字符串。这个答案的其余部分将集中在Python 2上。
您可以通过多种方式创建Unicode字符串:
>>> u'foo'
u'foo'
>>> unicode('foo') # Python 2 only
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
Ознакомьтесь с документацией
在大多数情况下,Unicode和非Unicode字符串在Python2上是可以互操作的。
您还会看到其他一些符号,比如“原始”符号r
,它告诉字符串不要解释反斜杠。这对于编写正则表达式非常有用。
>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'
在Python 2上,Unicode和非Unicode字符串可以相等:
>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True
但不是在Python 3上:
>>> x = u'asdf' # Python 3
>>> y = b'asdf' # b indicates bytestring
>>> x == y
False
发布于 2012-07-01 11:44:20
这是一个特性,而不是一个bug。
请参阅http://docs.python.org/howto/unicode.html,特别是“unicode type”部分。
https://stackoverflow.com/questions/11279331
复制相似问题