I am trying to write a Python script that writes x amount of files in a for loop, but at the moment it only writes a single file, item-{x}.json, instead of 110. How can I fix it?
import json
for x in range(110):
dictionary = {
"name":"Item #{x}",
"properties":{
"creators":[
{
"address":"H5ZQGeTv23nJRcFmEprRLuM9cTeJMajYvf2k1JhuLhE2",
"share":100
}
]
},
}
json_object = json.dumps(dictionary, indent = 2)
with open("item-{x}.json", "w") as outfile:
outfile.write(json_object)
>Solution :
use f-strings like this:
import json
for x in range(110):
dictionary = {
"name":f"Item #{x}",
"properties":{
"creators":[
{
"address":"H5ZQGeTv23nJRcFmEprRLuM9cTeJMajYvf2k1JhuLhE2",
"share":100
}
]
},
}
json_object = json.dumps(dictionary, indent = 2)
with open(f"item-{x}.json", "w") as outfile:
outfile.write(json_
object)