Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么是';u';字符串值前面的符号是什么意思?_Python_Google App Engine - Fatal编程技术网

Python 什么是';u';字符串值前面的符号是什么意思?

Python 什么是';u';字符串值前面的符号是什么意思?,python,google-app-engine,Python,Google App Engine,是的,简而言之,我想知道为什么我在我的键和值前面看到一个u 我正在渲染一个表单。该表单具有特定标签的复选框和ip地址的文本字段。我正在创建一个以键为标签的字典,这些键在list_键中硬编码,字典的值取自表单输入(list_值)。字典已创建,但对于某些值,它前面有u。以下是字典的示例输出: {u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}} 有人能解释一下我做错了什么吗。当我在pys

是的,简而言之,我想知道为什么我在我的键和值前面看到一个u

我正在渲染一个表单。该表单具有特定标签的复选框和ip地址的文本字段。我正在创建一个以键为标签的字典,这些键在list_键中硬编码,字典的值取自表单输入(list_值)。字典已创建,但对于某些值,它前面有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)
#/usr/bin/env python
导入webapp2
进口itertools
导入cgi
form=”“”
防火墙


概况1 概况2 概况3

选中此框以实施特定策略

允许广播

允许ARP

允许从外部地址到内部Web服务器的Web通信

允许DNS

阻止特定的Internet协议地址

""" dictionarymain={} 类MainHandler(webapp2.RequestHandler): def get(自我): 自我.回应.输出.写入(表格) def post(自我): #从表单中获取参数 profile=self.request.get('profiles') 广播=self.request.get('broadcast') arp=self.request.get('arp') webserver=self.request.get('webserver') dns=self.request.get('dns') ipaddr=self.request.get('ipaddr') #为上述参数创建字典 列表_值=[广播、arp、Web服务器、dns、ipaddr] list_key=['broadcast','arp','webserver','dns','ipaddr'] #self.response.headers['Content-Type']='text/plain' #self.response.out.write(配置文件) #使用itertools将两个列表映射到字典 adict=dict(zip(列表键、列表值)) self.response.headers['Content-Type']='text/plain' 自我、回应、输出、写入(adict) 如果配置文件不在dictionarymain中: dictionarymain[profile]={} dictionarymain[profile]=adict #self.response.headers['Content-Type']='text/plain' #self.response.out.write(字典输入) def escape_html: 返回cgi.escape(s,quote=True) app=webapp2.WSGIApplication([('/',MainHandler)], debug=True)
这是一个功能,而不是一个bug


具体请参见“unicode类型”部分

字符串值前面的“u”表示该字符串是Unicode字符串。Unicode是一种表示超出常规ASCII所能管理的字符数的方法。事实上,您看到的
u
意味着您在Python2上-字符串在Python3上默认为Unicode,但在Python2上,前面的
u
区分Unicode字符串。这个答案的其余部分将集中在Python2上

可以通过多种方式创建Unicode字符串:

>>> u'foo'
u'foo'
>>> unicode('foo') # Python 2 only
u'foo'
但真正的原因是要表示这样的内容():

在大多数情况下,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

你真正的问题是“为什么我在我的键和值前面看到一个
u
”?而且你没有在任何地方显示一开始就出现了错误。那是因为它们是unicode字符串:谢谢..我只是想澄清一下,据我所知,在以unicode表示的字符串操作字典时不会出现错误。@user1488987:正确。您可以在您的应用程序中使用unicodedict@jdi,伟大的示例字符串:)

>>> x = u'asdf' # Python 3
>>> y = b'asdf' # b indicates bytestring
>>> x == y
False