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 ?

5
  • You want to edit the json file dynamically? Commented Jan 1, 2020 at 12:29
  • No. He wants content to be interpreted as an f-string. Commented Jan 1, 2020 at 12:31
  • @wraithM17, I guess you can take a look at How to postpone/defer the evaluation of f-strings?. Commented Jan 1, 2020 at 12:33
  • you can still uses old format() - print(content.format(name=name)) Commented Jan 1, 2020 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)) Commented Jan 2, 2020 at 13:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.