社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在python中读取Json

Sai • 5 年前 • 1524 次点击  

这个问题已经有了答案:

我需要一个特别的 键:值 从json文件。

用于打开json文件的代码:

import json
data = repr(open('file.json', 'rb').read())
print(data)

输出( Json文件 ) :

b'b\'{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}\'\r\n'

从这个Json文件中,我需要提取一个 键:值 . 例如,我需要从文件中获取emotionsAll(密钥)。

我试着用:

print(data['emotionsAll'][0]['neutral']) 

我得到的错误:

Traceback (most recent call last):   File "C:\Users\HP\Downloads\Final Project\getfile.py", line 6, in <module>
    print(data['emotionsAll'][0]['neutral']) TypeError: string indices must be integers

那么,有没有其他方法可以得到一个特定的键:value?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49741
 
1524 次点击  
文章 [ 2 ]  |  最新文章 5 年前
bumblebee
Reply   •   1 楼
bumblebee    6 年前

尝试使用 json 包裹

内容 file.json

{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}

代码:

import json

with open('file.json', 'rb') as fp:
    json_data = json.load(fp)

print(json_data['objects'][0]['attributes']['emotionsAll']['neutral'])
chitown88
Reply   •   2 楼
chitown88    6 年前

有几件事:

1-那个 emotionsAll 钥匙在 objects 键,列表中的第一个元素 [0] , attributes 钥匙

2-您的json文件是以bytes前缀写入的,因此在读取时,它以 b' . 您可以a)通过解码/编码使该文件在不使用该模式的情况下写入,或者仅操作该字符串。

import json

data = repr(open('file.json', 'rb').read())

data = data.split('{', 1)[-1]
data = data.rsplit('}', 1)[0]

data = ''.join(['{', data, '}'])
jsonObj = json.loads(data)

print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 

输出:

print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 
0.0