通过将数据与模板进行比较从字符串中提取数据

时间:2016-10-20 13:33:59

标签: python-2.7 template-engine string-comparison data-extraction

str_1 = "Hi, my name is Kadamb Kaluskar. I live in India."

str_2 = "Hi, my name is {{first_name}} {{last_name}}. I live in {{country_name}}."

output = {

    'first_name' : 'Kadamb',
    'last_name' : 'Kaluskar',
    'country_name' : 'India'
}

是否有可用的库或模板引擎只需要两个参数,一个输入和一个模板并给出输出。并且模板不是固定的,因此无法在代码中进行硬编码。它需要是动态的。

如果它不可用,我如何通过编写自己的代码来实现它?

1 个答案:

答案 0 :(得分:1)

from pprint import pprint
import re

str_1 = "Hi, my name is Kadamb Kaluskar. I live in India."

str_2 = "Hi, my name is {{first_name}} {{last_name}}. I live in {{country_name}}."

opening_curlies = [m.start() for m in re.finditer('{{', str_2)]
opening_curlies = opening_curlies + [len(str_2)]
closing_curlies = [m.end() for m in re.finditer('}}', str_2)]
closing_curlies = [0] +closing_curlies

templates = [str_2[closing_curlies[idx]:opening_curlies[idx]].strip() for idx in range(len(opening_curlies)) if (str_2[closing_curlies[idx]:opening_curlies[idx]]!=' ' and str_2[closing_curlies[idx]:opening_curlies[idx]]!='')]

str_1_trimmed = str_1
str_2_trimmed = str_2
for template in templates:
    str_1_trimmed = str_1_trimmed.replace(template,'').replace('  ',' ').strip()
    str_2_trimmed = str_2_trimmed.replace(template, '').replace('  ',' ').strip()
vals = str_1_trimmed.strip().split(' ')
keys = str_2_trimmed.strip().split(' ')

output_param = {}
for item_idx in range(len(keys)):
    output_param[keys[item_idx]] = vals[item_idx]

pprint (output_param)

结果:

{'{{country_name}}': 'India',
 '{{first_name}}': 'Kadamb',
 '{{last_name}}': 'Kaluskar'}