在字符串值前面的“u”符号是什么意思?(复制)

时间:2022-05-20 20:18:38

This question already has an answer here:

这个问题已经有了答案:

Yes in short i would like to know why am I seeing a u in front of my keys and values.

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

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:

我正在呈现一个表单。该表单有特定标签的复选框和ip地址的一个文本字段。我正在创建一个带键的字典,它是在list_key中硬编码的标签,字典的值是从表单输入(list_value)中获取的。该字典是创建的,但它的前面有一些值。这是字典的示例输出:

{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

有人能解释一下我做错了什么吗?在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)

2 个解决方案

#1


128  

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.

在字符串值前面的“u”表示字符串被表示为unicode。字符串之前的字母被称为“字符串编码声明”。Unicode是一种表示比普通ascii可以管理的更多字符的方法。

You can convert a string to unicode multiple ways:

可以通过多种方式将字符串转换为unicode:

>>> 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.

您还会看到其他的符号,比如“原始”符号,用于告诉字符串不要解释任何特殊字符。这对于在python中执行正则表达式非常有用。

>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'

ASCII and Unicode strings can be logically equivalent:

ASCII和Unicode字符串在逻辑上是等价的:

>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True

#2


9  

This is a feature, not a bug.

这是一个特性,而不是bug。

See http://docs.python.org/howto/unicode.html, specifically the 'unicode type' section.

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

#1


128  

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.

在字符串值前面的“u”表示字符串被表示为unicode。字符串之前的字母被称为“字符串编码声明”。Unicode是一种表示比普通ascii可以管理的更多字符的方法。

You can convert a string to unicode multiple ways:

可以通过多种方式将字符串转换为unicode:

>>> 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.

您还会看到其他的符号,比如“原始”符号,用于告诉字符串不要解释任何特殊字符。这对于在python中执行正则表达式非常有用。

>>> 'foo\"'
'foo"'
>>> r'foo\"'
'foo\\"'

ASCII and Unicode strings can be logically equivalent:

ASCII和Unicode字符串在逻辑上是等价的:

>>> bird1 = unicode('unladen swallow')
>>> bird2 = 'unladen swallow'
>>> bird1 == bird2
True

#2


9  

This is a feature, not a bug.

这是一个特性,而不是bug。

See http://docs.python.org/howto/unicode.html, specifically the 'unicode type' section.

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