我需要一个特别的关键字:值来自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(Key)。你知道吗
我试过使用:
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
那么,有没有其他方法来获得一个特定的关键:价值?你知道吗
有几件事:
1-
emotionsAll
键在objects
键内,列表中的第一个元素[0]
,attributes
键2-json文件是用bytes前缀编写的,因此在读取时,它以
b'
开始字符串。您可以a)通过解码/编码在不使用该模式的情况下编写该文件,也可以仅操纵该字符串。你知道吗输出:
尝试使用
json
包file.json
的内容代码:
相关问题 更多 >
编程相关推荐