字符串值前面的“u”符号是什么意思?[重复]
- 2025-01-08 08:49:00
- admin 原创
- 165
问题描述:
是的,简而言之,我想知道为什么我会在我的键和值前面看到 au。
我正在渲染一个表单。该表单具有用于特定标签的复选框和一个用于 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)
解决方案 1:
字符串值前面的 'u' 表示该字符串是 Unicode 字符串。Unicode 是一种表示比普通 ASCII 所能管理的更多字符的方式。您看到的事实意味着u
您使用的是 Python 2 - 在 Python 3 上,字符串默认为 Unicode,但在 Python 2 上,u
前面的 区分了 Unicode 字符串。本回答的其余部分将重点介绍 Python 2。
您可以通过多种方式创建 Unicode 字符串:
>>> u'foo'
u'foo'
>>> unicode('foo') # Python 2 only
u'foo'
但真正的原因是为了表达这样的意思(此处有翻译):
>>> 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 字符串在 Python 2 上可以互操作。
您还会看到其他符号,例如“raw”符号,r
用于指示字符串不要解释反斜杠。这对于编写正则表达式非常有用。
>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\\"'
Unicode 和非 Unicode 字符串在 Python 2 上可以相等:
>>> 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
解决方案 2:
这是一个功能,而不是一个错误。
请参阅http://docs.python.org/howto/unicode.html,特别是“unicode 类型”部分。
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD