字典中的Python格式字典

字典中的Python格式字典,python,azure,dictionary,Python,Azure,Dictionary,我正试图在一本字典中格式化这本字典 完整代码: for image in url_images: detected_faces = face_client.face.detect_with_url(url=image, return_face_attributes=face_attributes) if not detected_faces: await ctx.send('No faces detected in this image') for fa

我正试图在一本字典中格式化这本字典

完整代码:

for image in url_images:
    detected_faces = face_client.face.detect_with_url(url=image, return_face_attributes=face_attributes)
    if not detected_faces:
        await ctx.send('No faces detected in this image')

    for face in detected_faces:
        age = str(face.face_attributes.age)
        age_fin = age.split(".")
        
        gender = str(face.face_attributes.gender)
        gender_fin = gender.split(".")

        await ctx.send('Facial attributes detected:\nAge: '+ age_fin[0] + 
        '\nGender: ' + gender_fin[1] + 
        '\nEmotion: ' + str(face.face_attributes.emotion))
Facial attributes detected:
Gender: male
Emotion: 0% anger, 0% contempt, 0% disgust, 0% fear, 0% happiness, 98% neutral, 0% sadness, 0% surprise
我通过代码获得的输出

Facial attributes detected:
Gender: male
Emotion: {'additional_properties': {}, 'anger': 0.001, 'contempt': 0.002, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.542, 'neutral': 0.455, 'sadness': 0.0, 'surprise': 0.0}
所需内容的格式示例:

for image in url_images:
    detected_faces = face_client.face.detect_with_url(url=image, return_face_attributes=face_attributes)
    if not detected_faces:
        await ctx.send('No faces detected in this image')

    for face in detected_faces:
        age = str(face.face_attributes.age)
        age_fin = age.split(".")
        
        gender = str(face.face_attributes.gender)
        gender_fin = gender.split(".")

        await ctx.send('Facial attributes detected:\nAge: '+ age_fin[0] + 
        '\nGender: ' + gender_fin[1] + 
        '\nEmotion: ' + str(face.face_attributes.emotion))
Facial attributes detected:
Gender: male
Emotion: 0% anger, 0% contempt, 0% disgust, 0% fear, 0% happiness, 98% neutral, 0% sadness, 0% surprise
我的尝试:

for image in url_images:
    detected_faces = face_client.face.detect_with_url(url=image, return_face_attributes=face_attributes)
    if not detected_faces:
        await ctx.send('No faces detected in this image')

    for face in detected_faces:
        age = str(face.face_attributes.age)
        age_fin = age.split(".")
        
        gender = str(face.face_attributes.gender)
        gender_fin = gender.split(".")

        await ctx.send('Facial attributes detected:\nAge: '+ age_fin[0] + 
        '\nGender: ' + gender_fin[1] + 
        '\nEmotion: ' + str(face.face_attributes.emotion))
Facial attributes detected:
Gender: male
Emotion: 0% anger, 0% contempt, 0% disgust, 0% fear, 0% happiness, 98% neutral, 0% sadness, 0% surprise
错误:
TypeError:“情感”对象不可下标
。下面是我的代码截图和尝试的解决方案

我正在使用的API 给定:

d = {'additional_properties': {}, 'anger': 0.001, 'contempt': 0.002, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.542, 'neutral': 0.455, 'sadness': 0.0, 'surprise': 0.0}
这将情感键格式化为所需字符串:

print(“,”.join([f'{v:.0%}{k}表示k,v在d.items()中,如果k!=“其他_属性”]))
输出:

0%愤怒、0%轻蔑、0%厌恶、0%恐惧、54%幸福、46%中立、0%悲伤、0%惊讶

Jim Xu回答

你能添加一个从API获得的示例吗?你能详细说明一下吗?我指的是从变量
face中获得的值。face\u属性
这个问题中有很多无关的信息。可以归结为你有一本字典:
{'additional_properties':{},“愤怒”:0.001,“蔑视”:0.002,“厌恶”:0.0,“恐惧”:0.0,“幸福”:0.542,“中立”:0.455,“悲伤”:0.0,“惊喜”:0.0}
,并想将其格式化为
情绪:0%愤怒,0%蔑视,0%厌恶,0%恐惧,0%快乐,98%中立,0%悲伤,0%的惊喜
?是的@marktolone这是真的是什么让你认为
Emotion
类像字典一样有一个
items()
方法?它没有,这就是为什么我得到错误
“Emotion”对象没有属性“items”
@Barmar我没有。我没有这个库,但是上面给出了
d
,它可以工作。始终可以执行
d=ast.literal\u eval(str(face.face\u attributes.emotion))
因为它是作为字典打印的:^)您可以更新您的答案吗@马克托洛宁