Python 在字典中迭代查找与文件名中前5个数字匹配的值

Python 在字典中迭代查找与文件名中前5个数字匹配的值,python,json,loops,dictionary,Python,Json,Loops,Dictionary,我试图根据json字典中存储的一些文件的相关信息来重命名这些文件。文件名当前类似于“00016_930831_fa.png”。我拥有字典中存储的所有文件的信息,包括“事实”如姓名(即“00016_930831_fa_a”)和“个人事实”如性别(即“男性”或“女性”) json文件如下所示,其中包含有关文件的事实: json\u数据={ “图像”: [ { “事实”: [ { “相对”:“数据/图像/00001/00001_930831_fa_a.p

我试图根据json字典中存储的一些文件的相关信息来重命名这些文件。文件名当前类似于“00016_930831_fa.png”。我拥有字典中存储的所有文件的信息,包括“事实”如姓名(即“00016_930831_fa_a”)和“个人事实”如性别(即“男性”或“女性”)

json文件如下所示,其中包含有关文件的事实:

json\u数据={
“图像”:
[        
{
“事实”:
[                
{
“相对”:“数据/图像/00001/00001_930831_fa_a.ppm.bz2”,
“光盘”:“1”,
“音高”:“0”,
“机头坐标”:“268”,
“主题”:“cfrS00001”,
“压缩”:“bzip2”,
“偏航”:“0”,
“右眼坐标”:“202”,
“环境”:“cfrE00001”,
“嘴坐标”:“266”,
“传感器”:“cfrN00002”,
“滚动”:“0”,
“胡子”:“不”,
“格式”:“ppm”,
“姿势”:“fa”,
“集合”:“cfrC00001”,
“光源”:“cfrI00001”,
“捕获时间”:“00:00:00”,
“阶段”:“cfrT00001”,
“捕获日期”:“1993年8月31日”,
“记录”:“cfrR00002”,
“天气”:“内部”,
“左眼坐标”:“326”,
“表达式”:“fa”,
“胡子”:“不”,
“眼镜”:“是的”
}
],
“基础”:“00001_930831_fa_a”,
“人与事实”:
[                
{
“性别”:“男性”,
“种族”:“白人”,
“id”:“cfrS00001”,
“yob”:“1943”
}
],
“根”:“…数据”,
“名称”:“00001”
}
]
}
。。。但这只是一张图像的数据,有数百张图像

对于我要重命名的每个文件(在下面的代码中称为“qualified_images”),我希望通过字典找到与该文件关联的性别,然后我希望在文件名的开头附加一个M(如果是男性)或一个F(如果是女性)

这是到目前为止我的代码。错误代码是一个属性错误,表示“列表”没有可归属的对象“键”


data = json.load(open('data.json'))
# the data is in the form of the json shown above.

# choosing, and making a list of, the neutral expression files that we want to search the disctionary for

from os import listdir

directory = '...'

qualified_people = list(fname for fname in listdir(directory) if fname.endswith('.png'))
#this is a list of about 100 photos, where their filenames and information are also stored in the json dictionary

# iterate through dictionary - if it finds an image that matches the name of a file on the qualified_people list , then look at the gender and change the filename accordingly
# trying to rename Mirta's photos to have M or F in them
import json
import dlib
import numpy as np
from skimage import io
import csv
import pandas as pd
import os
import glob, os


os.chdir("/Users/charlottepeart/Documents/Part 2/Python/")

#opening the big dictionary with all info

data = json.load(open('data.json'))
#print(data)

# choosing, and making a list of, the neutral expression files that we want to search the disctionary for

from os import listdir

directory = '/Users/charlottepeart/Documents/Part 2/Python/images_for_python/Mitra_stuff_again'

qualified_people = list(fname for fname in listdir(directory) if fname.endswith('.png'))
#print(qualified_people)

# iterate through dictionary - if it finds an image that matches the qualified people, then look at the gender
for i in qualified_people:
    for j in data:
        if i == data.key(name): 
            if data.key(gender) == female:
                i.append(data['F'])
            else:
                i.append(data['M']) ```

我已经在你的问题中更新了你的json输入,但是你想要的是这样的

下面是您的json数据
json_数据={
“图像”:
[        
{
“事实”:
[                
{
“相对”:“数据/图像/00001/00001_930831_fa_a.ppm.bz2”,
“光盘”:“1”,
“音高”:“0”,
“机头坐标”:“268”,
“主题”:“cfrS00001”,
“压缩”:“bzip2”,
“偏航”:“0”,
“右眼坐标”:“202”,
“环境”:“cfrE00001”,
“嘴坐标”:“266”,
“传感器”:“cfrN00002”,
“滚动”:“0”,
“胡子”:“不”,
“格式”:“ppm”,
“姿势”:“fa”,
“集合”:“cfrC00001”,
“光源”:“cfrI00001”,
“捕获时间”:“00:00:00”,
“阶段”:“cfrT00001”,
“捕获日期”:“1993年8月31日”,
“记录”:“cfrR00002”,
“天气”:“内部”,
“左眼坐标”:“326”,
“表达式”:“fa”,
“胡子”:“不”,
“眼镜”:“是的”
}
],
“基础”:“00001_930831_fa_a”,
“人与事实”:
[                
{
“性别”:“男性”,
“种族”:“白人”,
“id”:“cfrS00001”,
“yob”:“1943”
}
],
“根”:“…数据”,
“名称”:“00001”
}
]
}
def json_添加_前缀(json_数据):
对于json_数据['images']中的i:
如果我['person_facts'][0]['gender']=='Male':
i['facts'][0]['relative']='/'.join(i['facts'][0]['relative'].split('/')[:-1])+'/M'+i['facts'][0]['relative'].split('/')[-1]
如果我['person_facts'][0]['gender']=='Female':
i['facts'][0]['relative']='/'.join(i['facts'][0]['relative'].split('/')[:-1])+'/F_'+i['facts'][0]['relative'].split('/')[-1]
返回json_数据
打印(json_添加_前缀(json_数据))
'''
{'images':[{'facts':[{'relative':'data/images/00001/M_00001_930831_fa_a.ppm.bz2','disc':'1','pitch':'0','nose_坐标':'268','subject':'cfrS00001','compression':'bzip2','yaw':'0','右眼坐标':'202','environment':'cfrE00001','mouth坐标':'266传感器':'cfrN00002','roll':'0','bird':'No','format':'ppm','fa co':'拍摄地点:'cfrC00001','illuminant':'cfrI00001','capture_time':'00:00:00','stage':'cfrT00001','capture_date':'08/31/1993','recording':'cfrR00002','weather':'inside','left_eye_Coordination':'326','expression':'fa','fa'No','Glass':'Yes','Yes','base':'00001930831_fa a','Person930831':[{'gender':'Male','race':'White','id':'cfrS00001','yob':'1943'}],'root':'…data','name':'00001'}]}
'''

我不明白您想如何根据您的要求对性别进行分类。这里没有可以从包含值的JSON加载的数据类型method@nexla字典包含文件名和