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

2024-03-29 12:12:16 发布

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

我正试图根据存储在json字典中的一些文件的信息来重命名它们。文件名当前类似于“00016_”_法文.png". 我有一本字典里存储的所有文件的信息,包括“事实”如姓名(即“00016\u 930831\u fau a”)和“个人事实”如性别(即“男性”或“女性”)。你知道吗

json文件看起来是这样的,其中包含有关文件的事实:

json_data = {
    "images": 
    [        
        {
            "facts": 
            [                
                {
                    "relative": "data/images/00001/00001_930831_fa_a.ppm.bz2",
                    "disc": "1",
                    "pitch": "0",
                    "nose_coordinates": "268",
                    "subject": "cfrS00001",
                    "compression": "bzip2",
                    "yaw": "0",
                    "right_eye_coordinates": "202",
                    "environment": "cfrE00001",
                    "mouth_coordinates": "266",
                    "sensor": "cfrN00002",
                    "roll": "0",
                    "beard": "No",
                    "format": "ppm",
                    "pose": "fa",
                    "collection": "cfrC00001",
                    "illuminant": "cfrI00001",
                    "capture_time": "00:00:00",
                    "stage": "cfrT00001",
                    "capture_date": "08/31/1993",
                    "recording": "cfrR00002",
                    "weather": "inside",
                    "left_eye_coordinates": "326",
                    "expression": "fa",
                    "mustache": "No",
                    "glasses": "Yes"
                }
            ],
            "base": "00001_930831_fa_a",
            "person_facts": 
            [                
                {
                    "gender": "Male",
                    "race": "White",
                    "id": "cfrS00001",
                    "yob": "1943"
                }
            ],
            "root": "...data",
            "name": "00001"
        }
    ]
}

。。。但这只是一张图片的数据,有几百张图片。你知道吗

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

这是我目前的代码。错误代码是一个属性错误,并且表示“list”没有可归属的对象“key”。你知道吗


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']) ```

Tags: 文件oftheinimportjsonfordata
1条回答
网友
1楼 · 发布于 2024-03-29 12:12:16

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

your json data below
json_data = {
    "images": 
    [        
        {
            "facts": 
            [                
                {
                    "relative": "data/images/00001/00001_930831_fa_a.ppm.bz2",
                    "disc": "1",
                    "pitch": "0",
                    "nose_coordinates": "268",
                    "subject": "cfrS00001",
                    "compression": "bzip2",
                    "yaw": "0",
                    "right_eye_coordinates": "202",
                    "environment": "cfrE00001",
                    "mouth_coordinates": "266",
                    "sensor": "cfrN00002",
                    "roll": "0",
                    "beard": "No",
                    "format": "ppm",
                    "pose": "fa",
                    "collection": "cfrC00001",
                    "illuminant": "cfrI00001",
                    "capture_time": "00:00:00",
                    "stage": "cfrT00001",
                    "capture_date": "08/31/1993",
                    "recording": "cfrR00002",
                    "weather": "inside",
                    "left_eye_coordinates": "326",
                    "expression": "fa",
                    "mustache": "No",
                    "glasses": "Yes"
                }
            ],
            "base": "00001_930831_fa_a",
            "person_facts": 
            [                
                {
                    "gender": "Male",
                    "race": "White",
                    "id": "cfrS00001",
                    "yob": "1943"
                }
            ],
            "root": "...data",
            "name": "00001"
        }
    ]
}

def json_add_prefix(json_data):
    for i in json_data['images']:
        if i['person_facts'][0]['gender'] == 'Male':
            i['facts'][0]['relative'] = '/'.join(i['facts'][0]['relative'].split('/')[:-1])+ '/M_' + i['facts'][0]['relative'].split('/')[-1]
        if i['person_facts'][0]['gender'] == 'Female':
             i['facts'][0]['relative'] = '/'.join(i['facts'][0]['relative'].split('/')[:-1])+ '/F_' + i['facts'][0]['relative'].split('/')[-1]
    return json_data

print(json_add_prefix(json_data))
'''
{'images': [{'facts': [{'relative': 'data/images/00001/M_00001_930831_fa_a.ppm.bz2', 'disc': '1', 'pitch': '0', 'nose_coordinates': '268', 'subject': 'cfrS00001', 'compression': 'bzip2', 'yaw': '0', 'right_eye_coordinates': '202', 'environment': 'cfrE00001', 'mouth_coordinates': '266', 'sensor': 'cfrN00002', 'roll': '0', 'beard': 'No', 'format': 'ppm', 'pose': 'fa', 'collection': 'cfrC00001', 'illuminant': 'cfrI00001', 'capture_time': '00:00:00', 'stage': 'cfrT00001', 'capture_date': '08/31/1993', 'recording': 'cfrR00002', 'weather': 'inside', 'left_eye_coordinates': '326', 'expression': 'fa', 'mustache': 'No', 'glasses': 'Yes'}], 'base': '00001_930831_fa_a', 'person_facts': [{'gender': 'Male', 'race': 'White', 'id': 'cfrS00001', 'yob': '1943'}], 'root': '...data', 'name': '00001'}]}
'''

相关问题 更多 >