问题描述
我有这个json文本文件:
I have this json text file:
{
"EASW_KEY":" aaaaaaaaaaaaaaaaaaaaaaa",
"EASF_SESS":" aaaaaaaaaaaaaaaaaaaaaaa",
"PHISHKEY":" aaaaaaaaaaaaaaaaaaaaaaa",
"XSID":" aaaaaaaaaaaaaaaaaaaaaaa"
}
如果我这样解码它:
import json
data = open('data.txt').read()
data = json.loads(data)
print data['EASW_KEY']
我明白了:
u' aaaaaaaaaaaaaaaaaaaaaaa'
而不是简单地:
aaaaaaaaaaaaaaaaaaaaaaa
这u
是什么意思?
推荐答案
python中的字符串前面的'u'是什么意思?
以python字符串为前缀的字符称为python字符串编码声明.您可以在此处阅读有关它们的所有信息: https://docs.python. org/3.3/reference/lexical_analysis.html#encoding-declarations
The character prefixing a python string is called a python String Encoding declaration. You can read all about them here: https://docs.python.org/3.3/reference/lexical_analysis.html#encoding-declarations
u代表unicode.该插槽中的替代字母可以为r'foobar'
表示原始字符串,b'foobar'表示字节字符串.
The u stands for unicode. Alternative letters in that slot can be r'foobar'
for raw string and b'foobar' for byte string.
有关Python认为unicode的一些帮助: http://docs.python.org/howto/unicode.html
Some help on what Python considers unicode:http://docs.python.org/howto/unicode.html
然后要探索其本质,请运行以下命令:
Then to explore the nature of this, run this command:
type(u'abc')
返回:
<type 'unicode'>
如果在应该使用ascii时使用Unicode,或者在应该使用unicode时使用ascii,则当用户开始探索unicode空间"时,很可能会遇到冒泡实现.在运行时.
If you use Unicode when you should be using ascii, or ascii when you should be using unicode you are very likely going to encounter bubbleup implementationitis when users start "exploring the unicode space" at runtime.
例如,如果您将一个unicode字符串传递给facebook的api.fql(...)
函数,该函数表示它接受unicode,它将很高兴地处理您的请求,然后稍后返回未定义的结果,就像该函数成功遍历整个puke一样.地毯.
For example, if you pass a unicode string to facebook's api.fql(...)
function, which says it accepts unicode, it will happily process your request, and then later on return undefined results as if the function succeeded as it pukes all over the carpet.
如本文所定义:
来自python的FQL多查询失败,并出现了Unicode查询
某些unicode字符会导致生产中的代码被扣押,然后在整个地板上呕吐.因此,请确保您还可以,并且客户可以接受.
Some unicode characters are going to cause your code in production to have a seizure then puke all over the floor. So make sure you're okay and customers can tolerate that.
这篇关于python中的字符串前的"u"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!