下面是我的Python函数,我用它来呈现带有任何变量的yaml模板. 直到我把多行字符串传递给示例文件,它才能正常工作.
def render_template(template_path, context):
"""
Render Jinja2 template using the provided context.
"""
with open(template_path, 'r') as template_file:
template = Template(template_file.read())
return template.render(context)
下面是我调用上述函数的位置
if centralized_data is not None:
print("centralized_data is not None")
rendered_template = render_template(输入_template_path, context)
sample_data = yaml.safe_load(rendered_template)
# Ensure both sample_data and centralized_data are dictionaries
if isinstance(sample_data, dict) and isinstance(centralized_data, dict):
final_data = merge_data(sample_data, centralized_data)
else:
print("Error: Either sample_data or centralized_data is not a dictionary")
# Handle the error condition appropriately
final_data = {}
else:
print("centralized_data is None, using sample_data directly")
with open(输入_template_path, 'r') as template_file:
sample_data = yaml.safe_load(template_file)
final_data = sample_data
我认为问题发生在
sample_data = yaml.safe_load(rendered_template)
个
这是我的多字符串go 投掷,看起来像这样
输入
deployment:
annotations:
proxy.istio.io/config: |
concurrency: 1
concurr222ency: 1
concurrenc33y: 1
f: 3
和输出
deployment:
annotations:
proxy.istio.io/config: 'concurrency: 1
concurr222ency: 1
concurrenc33y: 1
f: 3
'
我也试过这样的东西,但没有帮助
proxy.istio.io/config: |>
proxy.istio.io/config: >-