在列表中删除你 [英] Removing u in list
问题描述
我已经阅读了删除列表中的字符'u',但我使用谷歌应用程序引擎,它似乎并没有工作!
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: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}']
推荐答案
'u'是字符串外部表示的一部分,意思是一个Unicode字符串,而不是一个te字符串。它不在字符串中,它是类型的一部分。
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.
例如,您可以使用相同的synax创建新的Unicode字符串字面值。例如:
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'
这会创建一个新的Unicode字符串其价值是瑞典三明治的词。您可以看到非英文字符由Unicode代码点表示,ö是 \xf6
,并且<是code> \ xe5 。 'u'前缀与您的示例中显示的相似,表示该字符串包含Unicode文本。
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.
为了摆脱这些,需要将Unicode字符串编码为面向字节的表示,如UTF-8。您可以使用例如:
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'
在这里,我们得到一个没有前缀'u'的新字符串,因为这是一个字节字符串。它包含表示Unicode字符串字符的字节,由于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.
这篇关于在列表中删除你的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!