2015-08-09 65 views
0

我想使用Python导入以下JSON文件:如何通过python解码unicode字符?

该文件称为new_json.json

{ "nextForwardToken": "f/3208873243596875673623625618474139659", "events": [ { "ingestionTime": 1045619, "timestamp": 1909000, "message": "2 32823453119 eni-889995t1 54.25.64.23 156.43.12.120 3389 23 6 342 24908 143234809 983246 ACCEPT OK" }] }

我有以下代码读取JSON文件,并删除中的Unicode字符:

JSON_FILE = "new_json.json" 
with open(JSON_FILE) as infile: 
    print infile 
    print '\n type of infile is \n', infile 
    data = json.load(infile) 
    str_data = str(data) # convert to string to remove unicode characters 
    wo_unicode = str_data.decode('unicode_escape').encode('ascii','ignore') 
    print 'unicode characters have been removed \n' 
    print wo_unicode 

print wo_unicode仍然在它的Unicode字符(即u)打印。

中的Unicode字符会努力对待JSON作为一本字典,当一个问题:

for item in data: 
    iden = item.get['nextForwardToken'] 

...导致错误:

AttributeError: 'unicode' object has no attribute 'get'

这在Python2工作0.7。有没有简单的方法呢?

+1

什么问题是unicode造成? –

+1

*“如何通过python删除json文件中的unicode字符?”*使文件完全空,这是唯一的方法。 (提示:**所有**字符都是Unicode字符,包括'a'和'q'。) –

+1

这个问题可能会更好地转化为'我如何解码unicode' - 答案可能取决于此类作为Windows上cmd shell中的当前代码页(如果您在该“OS”上运行该代码页)。 – Hannu

回答

1

错误无关使用Unicode,你试图把钥匙作为类型的字典,只需要使用data获得'nextForwardToken'

print data.get('nextForwardToken') 

当你遍历data,你在键,以便迭代'nextForwardToken'.get('nextForwardToken'),"events".get('nextForwardToken')等等,即使使用正确的语法,显然也不会起作用。

无论您通过data.get(u'nextForwardToken')data.get('nextForwardToken')访问,都将返回键的值:

In [9]: 'nextForwardToken' == u'nextForwardToken' 
Out[9]: True 
In [10]: data[u'nextForwardToken'] 
Out[10]: u'f/3208873243596875673623625618474139659' 
In [11]: data['nextForwardToken'] 
Out[11]: u'f/3208873243596875673623625618474139659' 
0

此代码会给你没有的Unicode值作为海峡

import json 
JSON_FILE = "/tmp/json.json" 
with open(JSON_FILE) as infile: 
    print infile 
    print '\n type of infile is \n', infile 
    data = json.load(infile) 
    print data 
    str_data = json.dumps(data) 
    print str_data 
相关问题