python - 在 python 中使用多级 JSON/dict/Template 推进字符串/JSON 格式化
问题描述
我想使用从第二个 json 映射的键替换 1 个 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'}
}
我尝试使用 Template 类使用 python 字符串格式化,但它似乎不支持复杂的 dict。
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)
它不工作。关于我还能尝试做什么的任何建议。
解决方案
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)
我正在使用 Django 模板引擎。默认情况下,它支持复杂的 JSON。
推荐阅读
- python - rdflib 不解析 RDF/XML 文件
- django - 如果我有一个项目的单独模型和与该项目相关的图像的单独模型,我如何将它们组合到一个查询中?
- python - 希望根据列表中的名称创建团队生成器-Python
- python - 连接 Python 和 R 脚本
- typescript - 向前传递参数时如何保留泛型类型?
- css - 如何在 CSS 中设置 flexbox 的 X 位置?
- r - pmap_dfr 使用具有多个输入的命名函数执行逐行(非逐列)操作
- mysql - sql中的比例
- suricata - 构建 Suricata 无法编译“der-parser”
- javascript - 如何将日期字符串格式化为全文 DD MM YYYY?