I am new to Python and I can´t find any way to execute this code.
I am using JSON generator and need to print formated code and add some changes to it. I have variables that have some values and I need to insert them into my code. So the final code will have exact formating but with added values.
hp = 50;
cp = 100;
wp = 40;
{
"721": {
"<policy_id>": {
"<asset_name>": {
"name": "<display_name>",
"image": "<ipfs_link>",
"mediaType": "<mime_type>",
"description": "<description>",
"Visual": "<rarity>",
"Health Points": "<hp (value of hp here)>",
"Combat Power": "<cp (value of cp here)>",
"Working Power": "<wp (value of wp here)>",
"files": [
{
"name": "<display_name>",
"mediaType": "<mime_type>",
"src": "<ipfs_link>"
}
]
}
Final output (I want to generate 100 of this same code with exact formating but with different hp wp and cp values.
{
"721": {
"<policy_id>": {
"<asset_name>": {
"name": "<display_name>",
"image": "<ipfs_link>",
"mediaType": "<mime_type>",
"description": "<description>",
"Visual": "<rarity>",
"Health Points": "50",
"Combat Power": "100",
"Working Power": "40",
"files": [
{
"name": "<display_name>",
"mediaType": "<mime_type>",
"src": "<ipfs_link>"
}
]
}
CodePudding user response:
You can use f-strings to painlessly wrap variables in strings, and json.dump/s()
can produce nice JSONs if an indent
argument is supplied:
import json
hp = 50
whatever = {"721":{"<policy id>":{"<asset name>":{"Health Points":f"{hp}"}}}}
print(json.dumps(whatever, indent=4))
will display
{ "721": { "<policy id>": { "<asset name>": { "Health Points": "50" } } } }
CodePudding user response:
# you don't need semi colon after variable
hp = 50
cp = 100
wp = 40
# use variable name directly without quotes
"Health Points": hp,
"Combat Power": cb,
"Working Power": wp,