Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing u in list

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 +"}"          test.append(a)       ast.literal_eval(json.dumps(test))     print test 

Final output:

[u'{email:[email protected],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}'] 
like image 664
Brian Li Avatar asked Mar 19 '12 15:03

Brian Li


People also ask

How do I remove you from a list?

In python, to remove Unicode ” u “ character from string then, we can use the replace() method to remove the Unicode ” u ” from the string.

How do I remove a character from a list in Python?

Remove All Occurrences of a Character From a String in Python Using the filter() Function. We can also use the filter() function with the join() method and lambda function to remove the occurrences of a character from a string in Python.

What is U prefix in string?

The prefix 'u' in front of the quote indicates that a Unicode string is to be created. If you want to include special characters in the string, you can do so using the Python Unicode-Escape encoding.


2 Answers

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 u'sm\xf6rg\xe5s' 

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") 'sm\xc3\xb6rg\xc3\xa5s' 

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.

like image 166
unwind Avatar answered Sep 21 '22 09:09

unwind


arr = [str(r) for r in arr] 

This basically converts all your elements in string. Hence removes the encoding. Hence the u which represents encoding gets removed Will do the work easily and efficiently

like image 31
mohdnaveed Avatar answered Sep 23 '22 09:09

mohdnaveed