问题:Python:json.loads返回以“ u”为前缀的项目
我将收到来自Obj-C的JSON编码字符串,并且正在解码一个伪字符串(目前),如下面的代码。我的输出结果是在每个项目前加上字符’u’:
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
JSON如何添加此Unicode字符?删除它的最佳方法是什么?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
I’ll be receiving a JSON encoded string form Obj-C, and I am decoding a dummy string (for now) like the code below. My output comes out with character ‘u’ prefixing each item:
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
How is JSON adding this unicode char? What’s the best way to remove it?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
回答 0
u-前缀仅表示您具有Unicode字符串。当您真正使用字符串时,它不会出现在您的数据中。不要被打印输出扔掉。
例如,尝试以下操作:
print mail_accounts[0]["i"]
你不会看到你。
The u- prefix just means that you have a Unicode string. When you really use the string, it won’t appear in your data. Don’t be thrown by the printed output.
For example, try this:
print mail_accounts[0]["i"]
You won’t see a u.
回答 1
回答 2
的 d3
下面打印件是您要查找打印件(这是转储和装载的组合):)
具有:
import json
d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""
d1 = json.loads(d) # Produces a dictionary out of the given string
d2 = json.dumps(d) # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d)) # 'dumps' gets the dict from 'loads' this time
print "d1: " + str(d1)
print "d2: " + d2
print "d3: " + d3
印刷品:
d1: {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}
d2: "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"
d3: {"Aa": 1, "cc": "False", "BB": "blabla"}
The d3
print below is the one you are looking for (which is the combination of dumps and loads) :)
Having:
import json
d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""
d1 = json.loads(d) # Produces a dictionary out of the given string
d2 = json.dumps(d) # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d)) # 'dumps' gets the dict from 'loads' this time
print "d1: " + str(d1)
print "d2: " + d2
print "d3: " + d3
Prints:
d1: {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}
d2: "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"
d3: {"Aa": 1, "cc": "False", "BB": "blabla"}
回答 3
该u
前缀意思是,那些字符串是unicode的,而不是8位的字符串。不显示u
前缀的最佳方法是切换到Python 3,默认情况下字符串为unicode。如果不是这种选择,则str
构造函数将从Unicode转换为8位,因此只需在结果上递归循环并转换unicode
为即可str
。但是,最好将字符串保留为unicode。
The u
prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u
prefix is to switch to Python 3, where strings are unicode by default. If that’s not an option, the str
constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode
to str
. However, it is probably best just to leave the strings as unicode.
回答 4
Unicode在这里是合适的类型。JSONDecoder文档描述了转换表,并声明将JSON字符串对象解码为Unicode对象
https://docs.python.org/2/library/json.html#encoders-and-decoders
JSON Python
==================================
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
“ encoding确定用于解释此实例解码的任何str对象的编码(默认为UTF-8)。”
Unicode is an appropriate type here. The JSONDecoder docs describe the conversion table and state that json string objects are decoded into Unicode objects
https://docs.python.org/2/library/json.html#encoders-and-decoders
JSON Python
==================================
object dict
array list
string unicode
number (int) int, long
number (real) float
true True
false False
null None
“encoding determines the encoding used to interpret any str objects decoded by this instance (UTF-8 by default).”
回答 5
那些附加在对象上的“ u”字符表示该对象以“ unicode”编码。
如果要从对象中删除那些“ u”字符,可以执行以下操作:
import json, ast
jdata = ast.literal_eval(json.dumps(jdata)) # Removing uni-code chars
让我们从python shell签出
>>> import json, ast
>>> jdata = [{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}]
>>> jdata = ast.literal_eval(json.dumps(jdata))
>>> jdata
[{'i': 'imap.gmail.com', 'p': 'aaaa'}, {'i': '333imap.com', 'p': 'bbbb'}]
Those ‘u’ characters being appended to an object signifies that the object is encoded in “unicode”.
If you want to remove those ‘u’ chars from your object you can do this:
import json, ast
jdata = ast.literal_eval(json.dumps(jdata)) # Removing uni-code chars
Let’s checkout from python shell
>>> import json, ast
>>> jdata = [{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}]
>>> jdata = ast.literal_eval(json.dumps(jdata))
>>> jdata
[{'i': 'imap.gmail.com', 'p': 'aaaa'}, {'i': '333imap.com', 'p': 'bbbb'}]
回答 6
当尝试使用Python logging
库捕获日志中的JSON数据时,为了调试和故障排除,我一直遇到这个问题。u
当您想要复制文本并将其粘贴到代码中的某个位置时,获取字符确实是个麻烦。
就像大家都会告诉你的那样,这是因为它是Unicode表示,它可能来自于您一开始就习惯json.loads()
从字符串中加载数据的事实。
如果要在日志中使用不带u
前缀的JSON表示形式,诀窍是json.dumps()
在注销之前使用它。例如:
import json
import logging
# Prepare the data
json_data = json.loads('{"key": "value"}')
# Log normally and get the Unicode indicator
logging.warning('data: {}'.format(json_data))
>>> WARNING:root:data: {u'key': u'value'}
# Dump to a string before logging and get clean output!
logging.warning('data: {}'.format(json.dumps(json_data)))
>>> WARNING:root:data: {'key': 'value'}
I kept running into this problem when trying to capture JSON data in the log with the Python logging
library, for debugging and troubleshooting purposes. Getting the u
character is a real nuisance when you want to copy the text and paste it into your code somewhere.
As everyone will tell you, this is because it is a Unicode representation, and it could come from the fact that you’ve used json.loads()
to load in the data from a string in the first place.
If you want the JSON representation in the log, without the u
prefix, the trick is to use json.dumps()
before logging it out. For example:
import json
import logging
# Prepare the data
json_data = json.loads('{"key": "value"}')
# Log normally and get the Unicode indicator
logging.warning('data: {}'.format(json_data))
>>> WARNING:root:data: {u'key': u'value'}
# Dump to a string before logging and get clean output!
logging.warning('data: {}'.format(json.dumps(json_data)))
>>> WARNING:root:data: {'key': 'value'}
回答 7
试试这个:
mail_accounts [0] .encode(“ ascii”)
Try this:
mail_accounts[0].encode(“ascii”)
回答 8
只需用单引号替换u …
print (str.replace(mail_accounts,"u'","'"))
Just replace the u’ with a single quote…
print (str.replace(mail_accounts,"u'","'"))