【问题标题】:What does the 'u' symbol mean in front of string values? [duplicate]字符串值前面的“u”符号是什么意思? [复制]
【发布时间】:2012-07-02 00:29:56
【问题描述】:

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

我正在渲染一个表单。该表单具有特定标签的复选框和一个用于 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)

【问题讨论】:

  • 您的实际问题是“为什么我在键和值前面看到u”?
  • 而且你一开始就不会在任何地方显示你遇到错误。
  • 那是因为它们是 unicode 字符串:stackoverflow.com/questions/599625/…

标签: python google-app-engine


【解决方案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'

但真正的原因是代表这样的东西(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 字符串在 Python 2 上是可互操作的。

您还会看到其他符号,例如“原始”符号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

【讨论】:

  • 谢谢你..只是为了说清楚,我明白我不会在字典上操作错误,字符串表示为 unicode。
  • @user1488987:正确。你的字典里可以有 unicode
  • @jdi,很好的示例字符串 :))
  • Ознакомьтесь с документацией ...:)
【解决方案2】:

这是一项功能,而不是错误。

参见http://docs.python.org/howto/unicode.html,特别是“unicode 类型”部分。

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2013-12-13
    • 2014-12-23
    • 2011-06-14
    • 1970-01-01
    • 2017-04-11
    • 2013-01-26
    相关资源
    最近更新 更多