我想使用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。有没有简单的方法呢?
什么问题是unicode造成? –
*“如何通过python删除json文件中的unicode字符?”*使文件完全空,这是唯一的方法。 (提示:**所有**字符都是Unicode字符,包括'a'和'q'。) –
这个问题可能会更好地转化为'我如何解码unicode' - 答案可能取决于此类作为Windows上cmd shell中的当前代码页(如果您在该“OS”上运行该代码页)。 – Hannu