我的列表包含以下字典。
[OrderedDict([('Employee Number', '1'), ('Employee Name', 'Ms. A'), ('RMG SPOC', 'X'), ('Total Experience (yrs)', '3.06'), ('Days Unallocated', '18'), ('Skill Details', 'Manual testing'), ('Contact Number', '1234')]), OrderedDict([('Employee Number', '2'), ('Employee Name', 'Mr. B'), ('RMG SPOC', 'Y'), ('Total Experience (yrs)', '2.51'), ('Days Unallocated', '28'), ('Skill Details', 'Manual Testing'), ('Contact Number', '2345')]), OrderedDict([('Employee Number', '3'), ('Employee Name', 'Mr. C'), ('RMG SPOC', 'Z'), ('Total Experience (yrs)', '1.86'), ('Days Unallocated', '9'), ('Skill Details', 'C++, Manual Testing, Oracle'), ('Contact Number', '4567')]), OrderedDict([('Employee Number', '4'), ('Employee Name', 'Mr. D'), ('RMG SPOC', 'xyz'), ('Total Experience (yrs)', '7.68'), ('Days Unallocated', '23'), ('Skill Details', 'Manual Testing, SQL, HCM'), ('Contact Number', '789')])]
我正在通过使用上述数据的for循环准备字典来将这些值推入数据库。
emp_data = {"employee_name" : data['Employee Name'],
"employee_number" : data['Employee Number'],
"date_added" : datetime.datetime.now(),
"rmg_spoc" : data['RMG SPOC'],
"status" : "To be evaluated",
"total_experience" : data['Total Experience (yrs)'],
"days_unallocated" : data['Days Unallocated'],
"skill_details" : data['Skill Details'],
"contact_number" : data['Contact Number'],
"reviewer" : "To be assigned",
"comments" : "To be added"}
我从excel / csv获取原始数据。只要键与提供的excel / csv中的数据匹配,此方法就可以正常工作。
如果excel / csv将“雇员姓名”作为“雇员姓名”或“雇员姓名”,则上述方法将不起作用。
有没有一种方法可以解决此问题,例如将“员工姓名”之类的键映射到与以下任何格式(“员工姓名”,“员工姓名”,“员工姓名”),“ rmg_spoc”相匹配的值”与任何格式(“ RMG”,“ rmg”,“ RMG SPOC”,“ rmg spoc”,“ rmg *”)匹配,“ total_experience”与任何格式(“总体验”,“总体验”, '* [E] [e] xperience *')。
答案 0 :(得分:1)
这似乎是一个不区分大小写的字典搜索(This SO question and all its duplicates)反复出现的问题的例子
帖子中建议的解决方案是使用包装器来像这样命令dict(或collections.OrderedDict):
import collections
class CaseInsensitiveDict(collections.Mapping):
def __init__(self, d):
self._d = d
self._s = dict((k.lower(), k) for k in d)
def __contains__(self, k):
return k.lower() in self._s
def __len__(self):
return len(self._s)
def __iter__(self):
return iter(self._s)
def __getitem__(self, k):
return self._d[self._s[k.lower()]]
def actual_key_case(self, k):
return self._s.get(k.lower())
在代码中,您只需使用此包装器包装字典即可,以便执行不区分大小写的键搜索:
data_items = [OrderedDict([('Employee Number', '1'), ('Employee Name', 'Ms. A'), ('RMG SPOC', 'X'), ('Total Experience (yrs)', '3.06'), ('Days Unallocated', '18'), ('Skill Details', 'Manual testing'), ('Contact Number', '1234')]), OrderedDict([('Employee Number', '2'), ('Employee Name', 'Mr. B'), ('RMG SPOC', 'Y'), ('Total Experience (yrs)', '2.51'), ('Days Unallocated', '28'), ('Skill Details', 'Manual Testing'), ('Contact Number', '2345')]), OrderedDict([('Employee Number', '3'), ('Employee Name', 'Mr. C'), ('RMG SPOC', 'Z'), ('Total Experience (yrs)', '1.86'), ('Days Unallocated', '9'), ('Skill Details', 'C++, Manual Testing, Oracle'), ('Contact Number', '4567')]), OrderedDict([('Employee Number', '4'), ('Employee Name', 'Mr. D'), ('RMG SPOC', 'xyz'), ('Total Experience (yrs)', '7.68'), ('Days Unallocated', '23'), ('Skill Details', 'Manual Testing, SQL, HCM'), ('Contact Number', '789')])]
data = CaseInsensitiveDict(data[0])
print(data['EmplOYee NAME'])
# should print 'Ms. A'
print(data['Employee NAME'])
# should print 'Ms. A'
print(data['EmploYee NAME'])
# should print 'Ms. A'
print(data['EmployeE NAME'])
# should print 'Ms. A'
print(data['Employee Name'])
# should print 'Ms. A'
答案 1 :(得分:0)
@ C.Nivs是正确的,下面的代码片段根据您的示例展示了其工作原理。
options = ('Employee Number','EMPLOYEE NUMBER','employee number')
for option in options:
assert option.strip().lower() == "employee number"
print("true")
答案 2 :(得分:0)
首先对输入中的键进行规范化怎么办? -也许会执行以下操作:
normalized_data = [{key.lower().replace(' ', '_'): val for key, val in datum.items()} for datum in data]
对于示例数据,您将获得:
[{'employee_number': '1', 'employee_name': 'Ms. A', 'rmg_spoc': 'X', 'total_experience_(yrs)': '3.06', 'days_unallocated': '18', 'skill_details': 'Manual testing', 'contact_number': '1234'},
{'employee_number': '2', 'employee_name': 'Mr. B', 'rmg_spoc': 'Y', 'total_experience_(yrs)': '2.51', 'days_unallocated': '28', 'skill_details': 'Manual Testing', 'contact_number': '2345'},
{'employee_number': '3', 'employee_name': 'Mr. C', 'rmg_spoc': 'Z', 'total_experience_(yrs)': '1.86', 'days_unallocated': '9', 'skill_details': 'C++, Manual Testing, Oracle', 'contact_number': '4567'},
{'employee_number': '4', 'employee_name': 'Mr. D', 'rmg_spoc': 'xyz', 'total_experience_(yrs)': '7.68', 'days_unallocated': '23', 'skill_details': 'Manual Testing, SQL, HCM', 'contact_number': '789'}]