从...开始:
input_1 = 'items.find({"repo": "lld-test-helm", "path": "customer-customer", "name": "customer-customer-0.29.3.tgz", "type": "file"})'
input_2 = 'items.find({"name": "customer-customer-0.29.3.tgz", "path": "customer-customer", "type": "file", "repo": "lld-test-helm"})'
items.find()
从字典周围剥离呼叫:
input_1 = input_1[11:-1]
input_2 = input_2[11:-1]
或者,如果您想更笼统:
input_1 = input_1[input_1.find('{'):input_1.rfind('}')+1]
input_2 = input_2[input_2.find('{'):input_2.rfind('}')+1]
至于从那一点确定两个字典字符串的相等性,必须将它们转换为实际的字典。
如果您愿意,可以使用 jez ( ast.literal_eval()
) 建议的方法,尽管我个人会json.loads()
为此目的使用:
import json
dict_1 = json.loads(input_1)
dict_2 = json.loads(input_2)
然后你只需比较两个字典:
dict_1 == dict_2
在这种情况下会返回True