在python中读取Json

2024-09-27 00:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个特别的关键字:值来自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

那么,有没有其他方法来获得一个特定的关键:价值?你知道吗


Tags: 文件代码jsondatatype关键字widthblack
2条回答

有几件事:

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

尝试使用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'])

相关问题 更多 >

    热门问题