I’m new to python
and programming in general, asking for help: What to run in python
code to print this data in multiple JSON
files (300 files) that every file change the “name” line from #1 to #300 and the JSON
files names will be like from 1.json
to 300.json
is it possible?
JavaScript
x
12
12
1
{
2
"description": "#",
3
"image": "#",
4
"name": "#1",
5
"attributes": [
6
{
7
"trait_type": "#",
8
"value": "#"
9
}
10
]
11
}
12
Advertisement
Answer
try this:
JavaScript
1
29
29
1
# library to work with json stuff
2
import json
3
4
# you can create dict first represent the json
5
json_dict = {
6
"description": "#",
7
"image": "#",
8
"name": "#1",
9
"attributes": [
10
{
11
"trait_type": "#",
12
"value": "#"
13
}
14
]
15
}
16
17
# define numbers of file you want to create
18
n = 300
19
for i in range(1, n+1):
20
# change the "name" atribute for every iteration
21
# you can use f-string method
22
json_dict["name"] = f"#{i}"
23
24
# save the dict to file
25
# again, use the f-string to name the file
26
with open(f"{i}.json", 'w') as json_file:
27
#json.dump() method save dict json to file
28
json.dump(json_dict, json_file)
29