1

So I'm having trouble printing some variable specified in a json file. Take a look at the code below and you'll understand.

"example.json"

{
    "to-print":{
        "content":"My name is {name}"
    }
}

"example.py"

import json
name = "wraithM17"
with open("example.json","r") as f:
    data = json.load(f)
    content = data["to-print"]["content"]
    print(f"{content}") 

So any way I can print that variable "name" like this ?

CarboxyDev
  • 47
  • 1
  • 5
  • You want to edit the json file dynamically? – Sharan Jan 01 '20 at 12:29
  • No. He wants `content` to be interpreted as an `f-string`. – accdias Jan 01 '20 at 12:31
  • @wraithM17, I guess you can take a look at [How to postpone/defer the evaluation of f-strings?](https://stackoverflow.com/questions/42497625/how-to-postpone-defer-the-evaluation-of-f-strings). – accdias Jan 01 '20 at 12:33
  • you can still uses old `format()` - `print(content.format(name=name)) ` – furas Jan 01 '20 at 13:11
  • Yeah so I already found the answer and forgot to check this question. So basically its "example.json" ``` { "to-print":{ "content":"My name is {}" } } ``` then python code to print is ` print(content.format(name)) – CarboxyDev Jan 02 '20 at 13:49

0 Answers0