简体   繁体   English

在 python 中使用多级 JSON/dict/Template 推进字符串/JSON 格式化

[英]Advance String/JSON formatting using a multilevel JSON/dict/Template in python

I want to replace text in 1 JSON using the keys mapped from a second json.我想使用从第二个 json 映射的键替换 1 JSON 中的文本。 Both the JSONs are dynamic and first JSON can be modified to use keys from second JSON.这两个 JSON 都是动态的,第一个 JSON 可以修改为使用来自第二个 JSON 的密钥。

text_to_replace = json.dumps([{"type": "data", "layerName": "THIS IS Mohit", "property": "Source Text", "expression": "time > 4 ? 'This is {groom.first_name}' : 'This is {groom.first_name}'", "composition": "01 Oliver"}, {"type": "data", "layerName": "THIS IS {bride.first_name}", "property": "Source Text", "expression": "time > 4 ? 'This is Aashi' : 'This is {bride.first_name}'", "composition": "02 Amelia"}, {"type": "data", "layerName": "January 2020", "property": "Source Text", "expression": "time > 4 ? '21st January 2021' : '21st January 2021'", "composition": "03 November"}, {"type": "data", "layerName": "JANUARY 2020", "property": "Source Text", "expression": "time > 4 ? '21st January 2021' : '21st January 2021'", "composition": "02 Date"}])




context = {'function_list': [
            {'name': 'Xbbd', 'venue': 'Xbxb\nXnx', 'time': '06:00 AM', 'date': '19th April 2020',
             'date_d_m_y_hyphen': '19-Apr-2020', 'timestamp': 1587234600.0, 'date_hindi': '19 अप्रैल 2020',
             'date_hindi_A': 'रविवार', 'date_hindi_B': 'अप्रैल', 'effective_date': '19th Apr 2020',
             'effective_day': 'Sunday', 'effective_time': '06:00 AM Onwards', 'effective_month': None}],
            'primary': {'first_name': 'Bride Name', 'last_name': 'Gshs', 'fathers_name': 'Sbsb',
                        'mothers_name': 'Bsb', 'grand_fathers_name': 'Sbdb', 'grand_mothers_name': 'Sb',
                        'effective_name': 'Bride Name Gshs',
                        'effective_parents_message': 'Daughter of \nSbsb & Bsb',
                        'effective_grand_parents_message': 'Grand Daughter of \nSbdb & Sb'},
            'secondary': {'first_name': 'Groom Name', 'last_name': 'Xbbs', 'fathers_name': 'Xbdb',
                          'mothers_name': 'Bdbd', 'grand_fathers_name': 'Xbxbnd', 'grand_mothers_name': 'Xbx',
                          'effective_name': 'Groom Name Xbbs',
                          'effective_parents_message': 'Son of \nXbdb & Bdbd',
                          'effective_grand_parents_message': 'Grand Son of \nXbxbnd & Xbx'},
            'other_details': {'special_message': '', 'special_message2': '', 'card_for': 'bride',
                              'invitation_from': 'zbzb', 'first_function_name': 'Xbbd', 'venue': 'Xbxb\nXnx',
                              'date': '19th Apr 2020', 'date_a': 'Sun', 'date_A': 'Sunday', 'date_d': '19',
                              'date_b': 'Apr', 'date_B': 'April', 'date_Y': '2020', 'day_th': 'th',
                              'time': '06:00 AM', 'date_hindi': '19 अप्रैल 2020', 'date_hindi_A': 'रविवार',
                              'date_hindi_B': 'अप्रैल', 'effective_date': '19th Apr 2020',
                              'effective_day': 'Sunday', 'effective_time': '06:00 AM Onwards',
                              'effective_month': None},
            'static_details': {'weds': 'weds'}
        }

I tried using python string formatting using Template class but it doesn't seems to support complex dict.我尝试使用模板 class 使用 python 字符串格式,但它似乎不支持复杂的字典。

from string import Template
s = Template("$bride['name'] likes what")
temp_json = {'bride': {'name': 'ishita'}}
print('temp_dict', type(temp_json))
temp = s.safe_substitute(temp_json)
print('string', s)
print('string', temp)

It is not working.它不工作。 Any suggestions about what else can I try to do this.关于我还能尝试做什么的任何建议。

def test(self):
    from django.template import Context, Template
    template = Template("My name is {{ my_name }}.")
    context = Context({"my_name": "Ujjwal"})
    string = template.render(context)
    print('string', string)

I am using Django template engine instead.我正在使用 Django 模板引擎。 It by default has support for complex JSON.它默认支持复杂的 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM