Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

python - How to replace placeholders inside dictionary with some values present in some other dictionary?

I hv a dictionary named as config:

config:

{
  "label": "{{label}}",
  "order": "{{order}}",
  "visible": "{{is_visible}}",
  "items": "{{items}}"
}

values:

{
   "label": "Some text here!",
   "order": 23,
   "is_visible": True,
   "items": [1,2,3,4]
}

final Output should be:

Output:

{
  "label": "Some text here!",
  "order": 23,
  "visible": True,
  "items": [1,2,3,4]
}

Note: Won't able to use Jinja library. because it will change the data type of every key in my output to string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use update on a dict if your keys are constant, in your example they are:

config={
  "label": "{{label}}",
  "order": "{{order}}",
  "visible": "{{is_visible}}",
  "items": "{{items}}"
}

values={
   "label": "Some text here!",
   "order": 23,
   "visible": True,
   "items": [1,2,3,4]
}

config.update(values)
config
{'label': 'Some text here!',
 'order': 23,
 'visible': True,
 'items': [1, 2, 3, 4]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...