字符串值前面的'u'符号是什么意思? [英] What does the 'u' symbol mean in front of string values?
问题描述
我正在渲染一个表单。该表格具有特定标签的复选框和IP地址的一个文本字段。我正在创建一个字典,其中键是在list_key中硬编码的标签,字典的值是从表单输入(list_value)中获取的。字典已创建,但在某些值的前面加上了u。这里是字典的输出示例:
{u'1':{'broadcast':u'on',' arp':'','webserver':'','ipaddr':u'','dns':''}}
有人可以解释我做错了什么。当我在pyscripter中模拟类似的方法时,我没有收到错误。欢迎任何改进代码的建议。谢谢
#!/ usr / bin / env python
导入webapp2
导入itertools
import cgi
form =
< form method =post>
FIREWALL
< br>< br> ;
< select name =profiles>
< option value =1> profile 1< / option>
< option value =2> 2< / option>
< option value =" 3>> profile 3< / option>
< / select>
< br>< br>
选中该框以实施特定政策
< br>< br>
< label>允许广播
< input type =checkboxname =broadcast >
< / label>
< br>< br>
< label>允许ARP
< input type =复选框 name =arp>
< / label>< br>< br>
< label>允许来自外部地址的网络流量到内部网络服务器
&l t; input type =checkboxname =webserver>
< / label>< br>< br>
< label>允许DNS
< input type =checkboxname =dns>
< / label>< br>< br>
< label>阻止特定的互联网协议地址
< input type =textname =ipaddr>
< / label>< br>< br>
< input type =submit>
< / form>
dictionarymain = {}
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out .write(form)
def post(self):
#从表单中获取参数$ b $ 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')
#为上述参数创建一个字典
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)
#将两个列表映射到字典使用itertools
adict = dict(zip(list_key,list_value))
self.response.headers ['Content-Type'] ='text / plain'
self.response.out.write(adict)
如果profile不在dictionarymain中:
dictionarymain [profile] = {}
dictionarymain [profile] = adict
#self.response.headers ['Content-输入'] ='text / plain'
#self.response.out.write(dictionarymain)
def escape_html(s):
return cgi.escape(s,quote = True)
app = webapp2.WSGIApplication([('/',MainHandler)],
debug = True)
$在字符串值前面的'u'表示字符串被表示为unicode 。字符串之前的字母称为字符串编码声明。 Unicode是一种代表更多字符的方式,而不是正常的ASCII字符。
您可以通过多种方式将字符串转换为unicode:
>>> u'foo'
u'foo'
>>> unicode('foo')
u'foo'
但是真正的原因是代表像这样( translation here ):
>>> val =u'Ознакомьтесьсдокументацией'
>>> val
u'\\\О\\\з\н\\\а\\\к\\\о\\\м\\\ь\\\т\\\е\\\с\\\ь \\\с \\\д\\ \\ u043e \\\к \\\у\\\м\\\е\\\н\\\т\\\а\\\ц\\\и\\\е\\\й'
>>>打印val
前往
绝大多数情况下,您不应该有任何错误在处理它们时不同于ASCII代码中的字符串。
还有其他的符号,例如告诉字符串不要解释任何特殊符号的原始符号字符。当在python中进行正则表达式时,这非常有用。
>>> 'foo \'''
'foo'
>>> r'foo \'''
'foo\\'''
ASCII和Unicode字符串可以在逻辑上相同:
>>> bird1 = unicode('unladen swallow')
>>> bird2 ='unladen swallow'
>>> bird1 == bird2
True
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 has been represented as unicode. Letters before strings here are called "String Encoding declarations". Unicode is a way to represent more characters than normal ascii can manage.
You can convert a string to unicode multiple ways:
>>> u'foo'
u'foo'
>>> unicode('foo')
u'foo'
But the real reason is to represent something like this (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
Ознакомьтесь с документацией
For the most part, you shouldn't have any errors in treating them different than ascii strings in this code.
There are other symbols you will see, such as the "raw" symbol for telling a string not to interpret any special characters. This is extremely useful when doing regular expression in python.
>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'
ASCII and Unicode strings can be logically equivalent:
>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True
这篇关于字符串值前面的'u'符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!