我有一个.CSV文件,如下所示:
NAME ES VALUE FILE_LOC NOTES
android_female HAPPY 25 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_happy_25.obj Slightly happy
android_female HAPPY 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_happy_50.obj happy
android_female HAPPY 75 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_happy_75.obj Joyful or glee, eyes squinty
android_female SAD 25 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_sad_25.obj Morose
android_female SAD 75 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_sad_75.obj Very Sad
android_female CONFUSED 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_confused_50.obj Bewildered
android_female SCARED 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_scared_50.obj Frightened
android_female TIRED 25 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_tired_25.obj Slightly run down
android_female TIRED 75 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_tired_75.obj Very tired
android_female SURPRISED 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_surprised_50.obj Semi shocked
android_female EMBARRASSED 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_embarrassed_50.obj Reddened cheeks
android_female KISS 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_kiss_50.obj Kissy face, eyes closed
android_female MISCHIEVOUS 50 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_mischievous_50.obj Smirking and shifty eyes
android_female PAIN 25 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_pain_25.obj Slight pain, grimace
android_female PAIN 75 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_pain_75.obj Severe pain, eyes closed
android_female ANGRY 25 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_angry_25.obj Upset, brow furrowed
android_female ANGRY 75 C:\Users\jason\Documents\3Dobjects\poses\androidfemale\androidfemale_angry_75.obj Very angry, brows down
相当简单。要从.CSV到dict,我有以下代码:
layouts_schema_file_name ='C:\\Users\\jason\\Documents\\RAY\\char_es_schemas\\ANIBOT_CHAR_ES_SCHEMA.csv'
full_path_to_file = layouts_schema_file_name
print('============ Importing LAYOUTS schema from: ', full_path_to_file , ' ==============')
openfile = open(full_path_to_file)
reader = csv.DictReader(openfile)
print('Printing Character Emotional States reader file:')
print(reader)
char_es_schema = {}
for group1, records1 in itertools.groupby(reader, key=operator.itemgetter("NAME")):
char_es_schema[group1] = {}
for group2, records2 in itertools.groupby(records1, key=operator.itemgetter("ES")):
char_es_schema[group1][group2] = list(records2)
#for group3, records3 in itertools.groupby(records2, key=operator.itemgetter("VALUE")):
# char_es_schema[group2][group3] = list(records3)
这会产生以下(可能太复杂)的dict,它旨在以结构化的方式存储键和值:
{'android_female': {'ANGRY': [{'ES': 'ANGRY',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_angry_25.obj',
'NAME': 'android_female',
'NOTES': 'Upset, brow furrowed',
'VALUE': '25'},
{'ES': 'ANGRY',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_angry_75.obj',
'NAME': 'android_female',
'NOTES': 'Very angry, brows down',
'VALUE': '75'}],
'CONFUSED': [{'ES': 'CONFUSED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_confused_50.obj',
'NAME': 'android_female',
'NOTES': 'Bewildered',
'VALUE': '50'}],
'EMBARRASSED': [{'ES': 'EMBARRASSED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_embarrassed_50.obj',
'NAME': 'android_female',
'NOTES': 'Reddened cheeks',
'VALUE': '50'}],
'HAPPY': [{'ES': 'HAPPY',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_happy_25.obj',
'NAME': 'android_female',
'NOTES': 'Slightly happy',
'VALUE': '25'},
{'ES': 'HAPPY',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_happy_50.obj',
'NAME': 'android_female',
'NOTES': 'happy',
'VALUE': '50'},
{'ES': 'HAPPY',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_happy_75.obj',
'NAME': 'android_female',
'NOTES': 'Joyful or glee, eyes squinty',
'VALUE': '75'}],
'KISS': [{'ES': 'KISS',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_kiss_50.obj',
'NAME': 'android_female',
'NOTES': 'Kissy face, eyes closed',
'VALUE': '50'}],
'MISCHIEVOUS': [{'ES': 'MISCHIEVOUS',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_mischievous_50.obj',
'NAME': 'android_female',
'NOTES': 'Smirking and shifty eyes',
'VALUE': '50'}],
'PAIN': [{'ES': 'PAIN',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_pain_25.obj',
'NAME': 'android_female',
'NOTES': 'Slight pain, grimace',
'VALUE': '25'},
{'ES': 'PAIN',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_pain_75.obj',
'NAME': 'android_female',
'NOTES': 'Severe pain, eyes closed',
'VALUE': '75'}],
'SAD': [{'ES': 'SAD',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_sad_25.obj',
'NAME': 'android_female',
'NOTES': 'Morose',
'VALUE': '25'},
{'ES': 'SAD',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_sad_75.obj',
'NAME': 'android_female',
'NOTES': 'Very Sad',
'VALUE': '75'}],
'SCARED': [{'ES': 'SCARED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_scared_50.obj',
'NAME': 'android_female',
'NOTES': 'Frightened',
'VALUE': '50'}],
'SURPRISED': [{'ES': 'SURPRISED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_surprised_50.obj',
'NAME': 'android_female',
'NOTES': 'Semi shocked',
'VALUE': '50'}],
'TIRED': [{'ES': 'TIRED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_tired_25.obj',
'NAME': 'android_female',
'NOTES': 'Slightly run down',
'VALUE': '25'},
{'ES': 'TIRED',
'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_tired_75.obj',
'NAME': 'android_female',
'NOTES': 'Very tired',
'VALUE': '75'}]}}
我正在尝试迭代这个嵌套的dict,以便匹配NAME和ES级别以及最后一级的VALUE(它实际上是一个带有dict的列表)。目的是简单地拉取file_loc以便以后使用,使用NAME,ES和VALUE来拉取它。它应该是一个3键连接字段。
很难做到这一点。我怀疑我的词汇过于复杂,会不会有任何想法。
这是我的迭代代码,现在不起作用:
for actors in char_es_schema.keys():
if found_it == True:
print('Found character emotional state...')
continue
for emotions in actors:
for poses in emotions:
print('Current ES state and value we are searching is: ',char_es_schema[actor_name][curr_es][0]['ES'] ,char_es_schema[actor_name][curr_es][0]['VALUE'])
if char_es_schema[actor_name][curr_es][0]['VALUE'] == curr_es_level:
file_loc = char_es_schema[actor_name][curr_es][0]['FILE_LOC']#!!!!!! what code goes here JMR 12/4
print('Found file location for emotional state:', curr_es, curr_es_level, ' - : ', file_loc)
found_it = True
if found_it == True:
-do some stuff-
有什么想法吗?我认为我使我的词汇太复杂,难以迭代,但会欣赏任何想法。
答案 0 :(得分:0)
怎么回事?
def select(schema, name, es, value):
emotionGrp = schema[name][es]
return [x for x in emotionGrp if x['VALUE']==value];
print(select(data, 'android_female', 'PAIN', '75'))
data
是您在问题中提供的数据结构。
使用示例数据运行此代码将返回:
[{'NOTES': 'Severe pain, eyes closed', 'VALUE': '75', 'NAME': 'android_female', 'ES': 'PAIN', 'FILE_LOC': 'C:\\Users\\jason\\Documents\\3Dobjects\\poses\\androidfemale\\androidfemale_pain_75.obj'}]
可以进一步简化为:
selectedAndroids = [x for x in schema[name][es] if x['VALUE']==value];
显然缺少的是对数据模型中不存在的密钥的任何检查。唔...
selectedAndroids = [x for x in schema.get(name, {}).get(es, {}) if x.get('VALUE', None)==value]