由于属性的名称不同,我需要将键值的键与正则表达式进行匹配。你知道吗
可能的名称在dict中定义:
MyAttr = [
('ref_nr', 'Reference|Referenz|Referenz-Nr|Referenznummer'),
('color', 'Color|color|tinta|farbe|Farbe'),
]
从另一个dict中的项导入属性:
ImportAttr = [
('Referenz', 'Ref-Val'),
('color', 'red'),
]
现在我想返回import属性的值,如果它是一个已知的属性(在我的第一个dict MyAttr中定义)匹配有问题的属性的不同拼写。你知道吗
for key, value in ImportAttr:
if key == "Referenz-Nr" : ref = value
if key == "Farbe" : color = value
目标是返回可能属性的值(如果该属性是已知的)。你知道吗
print(ref)
print(color)
如果“referez Nr”和“Farbe”是已知属性,则应返回值。你知道吗
很明显,这个伪代码不起作用,我只是无法理解一个实现regex的函数来进行键搜索。你知道吗
我不清楚,但也许你想要:
拆分可以通过分隔符(“|”字符)将字符串拆分为一个字符串列表,然后可以检查该列表中是否有键。你知道吗
下面的解决方案有点棘手。如果不想将位置硬编码到源代码中,可以使用locals()。你知道吗
如果需要,还可以使用
pandas
以这种方式解决大型数据集的这个问题。你知道吗相关问题 更多 >
编程相关推荐