Take the 2-minute tour ×

I have read up on remove the character 'u' in a list but I am using google app engine and it does not seem to work!

def get(self):
    players = db.GqlQuery("SELECT * FROM Player")
    print players
    playerInfo  = {}

    test = []

    for player in players:
        email =  player.email
        gem =  str(player.gem)
        a = "{email:"+email + ",gem:" +gem +"}"


    print test

Final output:

[u'{email:test@gmail.com,gem:0}', u'{email:test,gem:0}', u'{email:test,gem:0}', u'{email:test,gem:0}', u'{email:test,gem:0}', u'{email:test1,gem:0}']
share|improve this question
the character "u" isn't in the list, it's in the repr of a unicode string, which is what's printed if you try to print a whole list. –  Wooble Mar 19 '12 at 15:39
The u denotes Unicode strings. It doesn't seem to be a problem by itself that the list contains Unicode strings, so what's your actual issue? –  Sven Marnach Mar 19 '12 at 15:39
The code ast.literal_eval(json.dumps(test)) calculates a value and then throws it away. –  Karl Knechtel Mar 19 '12 at 15:42
add comment

3 Answers

up vote 7 down vote accepted

That 'u' is part of the external representation of the string, meaning it's a Unicode string as opposed to a byte string. It's not in the string, it's part of the type.

As an example, you can create a new Unicode string literal by using the same synax. For instance:

>>> sandwich = u"smörgås"
>>> sandwich

This creates a new Unicode string whose value is the Swedish word for sandwich. You can see that the non-English characters are represented by their Unicode code points, ö is \xf6 and å is \xe5. The 'u' prefix appears just like in your example to signify that this string holds Unicode text.

To get rid of those, you need to encode the Unicode string into some byte-oriented representation, such as UTF-8. You can do that with e.g.:

>>> sandwich.encode("utf-8")

Here, we get a new string without the prefix 'u', since this is a byte string. It contains the bytes representing the characters of the Unicode string, with the Swedish characters resulting in multiple bytes due to the wonders of the UTF-8 encoding.

share|improve this answer
add comment

You don't "remove the character 'u' from a list", you encode Unicode strings. In fact the strings you have are perfectly fine for most uses; you will just need to encode them appropriately before outputting them.

share|improve this answer
add comment

The u means the strings are unicode. Translate all the strings to ascii to get rid of it:

a.encode('ascii', 'ignore')
share|improve this answer
add comment

Your Answer


