¿Qué significa el símbolo 'u' delante de los valores de cadena? [duplicar]

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">
    <select name="profiles">
        <option value="1">profile 1</option>
        <option value="2">profile 2</option>
        <option value="3">profile 3</option>
    Check the box to implement the particular policy

    <label> Allow Broadcast
        <input type="checkbox" name="broadcast">

    <label> Allow ARP
        <input type="checkbox" name="arp">

    <label> Allow Web traffic from external address to internal webserver
        <input type="checkbox" name="webserver">

    <label> Allow DNS
        <input type="checkbox" name="dns">

    <label> Block particular Internet Protocol  address
        <input type="text" name="ipaddr">

    <input type="submit">   

class MainHandler(webapp2.RequestHandler):  
    def get(self):

    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'

        # map two list to a dictionary using itertools
        adict = dict(zip(list_key,list_value))
        self.response.headers['Content-Type'] ='text/plain'

        if profile not in dictionarymain:
            dictionarymain[profile]= {}
        dictionarymain[profile]= adict

        #self.response.headers['Content-Type'] ='text/plain'

        def escape_html(s):
            return cgi.escape(s, quote =True)

app = webapp2.WSGIApplication([('/', MainHandler)],

preguntado el 01 de julio de 12 a las 04:07

Is your actual question "Why am I seeing a u in front of my keys and values"? -

And you don't show anywhere that you are getting an error in the first place. -

That's because they're unicode strings: stackoverflow.com/questions/599625/… -

2 Respuestas

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'
>>> unicode('foo') # Python 2 only

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, 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\"'
>>> r'foo\"'

Unicode and non-Unicode strings can be equal on Python 2:

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

but not on Python 3:

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

Respondido 08 Jul 20, 21:07

Thank you ..just to make it clear, what i understand that i will not get error operating on dictionary with string represented as unicode. - user1488987

@user1488987: Correct. You can have unicode in your dict - jdi

@jdi, great example string :)) - Iulian Onofrei

Ознакомьтесь с документацией ... :) - Flamenco

Esto es una característica, no un error.

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

Respondido 01 Jul 12, 04:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.