Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带前缀的Python字典键/值-what';它的前缀是什么?_Python_Unicode_Python Unicode - Fatal编程技术网

带前缀的Python字典键/值-what';它的前缀是什么?

带前缀的Python字典键/值-what';它的前缀是什么?,python,unicode,python-unicode,Python,Unicode,Python Unicode,我最近看到一个Python dict是这样的: test1 = {u'user':u'user1', u'user_name':u'alice'} 这让我有点困惑,键/值对之前的u是干什么的?它是某种前缀吗?这有什么不同: test2 = {'user':'user1', 'user_name':'alice'} 我试着同时使用test1和test2;他们看起来一点也不不同。谁能解释一下前缀是什么意思吗 >>> test1 = {u'user':u'user1', u'us

我最近看到一个Python dict是这样的:

test1 = {u'user':u'user1', u'user_name':u'alice'}
这让我有点困惑,键/值对之前的
u
是干什么的?它是某种前缀吗?这有什么不同:

test2 = {'user':'user1', 'user_name':'alice'}
我试着同时使用test1和test2;他们看起来一点也不不同。谁能解释一下前缀是什么意思吗

>>> test1 = {u'user':u'user1', u'user_name':u'alice'}
>>> test2 = {'user':'user1', 'user_name':'alice'} 
>>> print test1[u'user']
user1
>>> print test1['user']
user1
>>> print test2['user']
user1
>>> print test2[u'user']

在Python2中,必须强制Unicode字符保留为Unicode

因此,
u
阻止文本转换为ASCII。(保留为unicode)

例如,这在Python 2中不起作用:

'ô SO'.upper() == 'Ô SO''
除非您这样做:

u'ô SO'.upper() == 'Ô SO'
您可以阅读更多关于此的内容:


一些历史记录:

u'unicode string'
将使字符串成为一种类型,如果没有前缀,字符串是ASCII类型字符串
'ASCII string'
u
代表。当您访问数据库时,他们会将您的字符串转换为unicode,所以,除非你真的到了与unicodes打交道的地步,否则你不应该为此而感到压力。谢谢:)有没有关于这方面的文档?你能给我一个代码示例吗?我可以将文本转换为ASCII。@Shengjie您可以使用将字符转换为ASCII值。例如,
ord('a')
将返回
97