字符串值前面的“u"符号是什么意思? [英] What does the 'u' symbol mean in front of string values?
问题描述
是的,简而言之,我想知道为什么我会在我的键和值前面看到一个 u.
我正在渲染一个表单.该表单具有用于特定标签的复选框和用于 ip 地址的一个文本字段.我正在创建一个字典,其键是在 list_key 中硬编码的标签,字典的值取自表单输入 (list_value).字典已创建,但对于某些值,它以 u 开头.这是字典的示例输出:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}}
有人可以解释一下我做错了什么吗?当我在 pyscripter 中模拟类似的方法时,我没有收到错误消息.欢迎任何改进代码的建议.谢谢
#!/usr/bin/env python导入 webapp2导入迭代工具导入 cgi形式="""<form method="post">防火墙<br><br><选择名称=个人资料"><option value="1">配置文件 1</option><option value="2">配置文件 2</option><option value="3">配置文件3</option></选择><br><br>选中此框以实施特定政策<br><br><标签>允许广播<input type="checkbox" name="broadcast">标签><br><br><标签>允许 ARP<input type="checkbox" name="arp"></label><br><br><标签>允许从外部地址到内部网络服务器的网络流量<input type="checkbox" name="webserver"></label><br><br><标签>允许 DNS<input type="checkbox" name="dns"></label><br><br><标签>阻止特定的 Internet 协议地址<input type="text" name="ipaddr"></label><br><br><输入类型=提交"></表单>"""字典主={}类 MainHandler(webapp2.RequestHandler):定义获取(自我):self.response.out.write(form)定义帖子(自我):#从表单中获取参数profile = self.request.get('profiles')广播 = self.request.get('广播')arp = self.request.get('arp')webserver = self.request.get('webserver')dns =self.request.get('dns')ipaddr = self.request.get('ipaddr')# 为上述参数创建字典list_value =[广播,arp,网络服务器,dns,ipaddr]list_key =['broadcast', 'arp', 'webserver', 'dns', 'ipaddr']#self.response.headers['Content-Type'] ='text/plain'#self.response.out.write(配置文件)# 使用 itertools 将两个列表映射到一个字典adict = dict(zip(list_key,list_value))self.response.headers['Content-Type'] ='text/plain'self.response.out.write(adict)如果配置文件不在字典中:字典main[配置文件]= {}字典main[profile]= adict#self.response.headers['Content-Type'] ='text/plain'#self.response.out.write(dictionarymain)def escape_html(s):返回 cgi.escape(s, quote =True)app = webapp2.WSGIApplication([('/', MainHandler)],调试=真)
字符串值前面的 'u' 表示该字符串是 Unicode 字符串.Unicode 是一种表示比普通 ASCII 可以管理的更多字符的方法.您看到 u
的事实意味着您使用的是 Python 2 - Python 3 上的字符串默认为 Unicode,但在 Python 2 上,前面的 u
可以区分Unicode 字符串.本答案的其余部分将重点关注 Python 2.
您可以通过多种方式创建 Unicode 字符串:
<预><代码>>>>你'福'你'福'>>>unicode('foo') # 仅适用于 Python 2你'福'但真正的原因是表示这样的东西(在这里翻译):
<预><代码>>>>val = u'Ознакомьтесь с документацией'>>>值u'u041eu0437u043du0430u043au043eu043cu044cu0442u0435u0441u044cu0441u0434u043eu04304u4030403040304030404030403040304043u0435u0439'>>>打印值Ознакомьтесь с документацией在大多数情况下,Unicode 和非 Unicode 字符串在 Python 2 上是可互操作的.
您还会看到其他符号,例如原始"符号符号 r
用于告诉字符串不要解释反斜杠.这对于编写正则表达式非常有用.
Unicode 和非 Unicode 字符串在 Python 2 上可以相等:
<预><代码>>>>Bird1 = unicode('空载燕子')>>>Bird2 = '空载燕子'>>>鸟 1 == 鸟 2真的但不是在 Python 3 上:
<预><代码>>>>x = u'asdf' # Python 3>>>y = b'asdf' # b 表示字节串>>>x == y错误的Yes in short i would like to know why am I seeing a u in front of my keys and values.
I am rendering a form. The form has check-box for the particular label and one text field for the ip address. I am creating a dictionary with keys being the label which are hardcoded in the list_key and values for the dictionary are taken from the form input (list_value). The dictionary is created but it is preceded by u for some values. here is the sample output for the dictionary:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}}
can someone please explain what I am doing wrong. I am not getting the error when i simulate similar method in pyscripter. Any suggestions to improve the code are welcome. Thank you
#!/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)
The 'u' in front of the string values means the string is a Unicode string. Unicode is a way to represent more characters than normal ASCII can manage. The fact that you're seeing the u
means you're on Python 2 - strings are Unicode by default on Python 3, but on Python 2, the u
in front distinguishes Unicode strings. The rest of this answer will focus on Python 2.
You can create a Unicode string multiple ways:
>>> u'foo'
u'foo'
>>> unicode('foo') # Python 2 only
u'foo'
But the real reason is to represent something like this (translation here):
>>> val = u'Ознакомьтесь с документацией'
>>> val
u'u041eu0437u043du0430u043au043eu043cu044cu0442u0435u0441u044c u0441 u0434u043eu043au0443u043cu0435u043du0442u0430u0446u0438u0435u0439'
>>> print val
Ознакомьтесь с документацией
For the most part, Unicode and non-Unicode strings are interoperable on Python 2.
There are other symbols you will see, such as the "raw" symbol r
for telling a string not to interpret backslashes. This is extremely useful for writing regular expressions.
>>> 'foo"'
'foo"'
>>> r'foo"'
'foo\"'
Unicode and non-Unicode strings can be equal on Python 2:
>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True
but not on Python 3:
>>> x = u'asdf' # Python 3
>>> y = b'asdf' # b indicates bytestring
>>> x == y
False
这篇关于字符串值前面的“u"符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!