英文:
generate JSON file content
问题
以下是Python代码示例,可生成包含与file-1相同内容的另一个JSON文件,其中每次用户名和优先级不同:
import json
# 从file-1中读取JSON数据
with open('file-1.json', 'r') as infile:
json_data = json.load(infile)
# 创建一个新JSON对象,用于存储重复的数据
new_json_object = {"Aps": []}
# 重复100次
for i in range(1, 101):
# 复制原始数据
new_entry = json_data["Aps"][0].copy()
# 更新用户名和优先级
new_entry["attributes"]["name"] = f"user{i}"
new_entry["attributes"]["priority"] = str(100 - i * 10)
# 添加到新JSON对象中
new_json_object["Aps"].append(new_entry)
# 写入新JSON数据到文件
with open('output.json', 'w') as outfile:
json.dump(new_json_object, outfile, ensure_ascii=False, indent=4)
你可以将原始JSON数据存储在名为file-1.json
的文件中,然后运行这段代码以生成包含所需内容的新JSON文件。新文件将被命名为output.json
。请确保你的Python环境中已经安装了JSON模块。
英文:
I am a beginner with JSON.
I have the following in my JSON file (file-1) :
{
"Aps": [
{
"type": "opo",
"price_min": 7,
"price_max":10,
"app_time": 0,
"arguments": {
"prices": 15,
"apps_num": "112"
},
"attributes": {
"name":"user1",
"priority":"100"
}
}
}
How write python code that generates another JSON file that contain the same content of file-1 but duplicated 100 time in each time the name of user is different user2, user3 ... user100 and also it's priority.
I have tried the following but it is not working :
for lp in range(100):
with open("sample.json", "w") as outfile:
outfile.write(json_object)
but it is not working ..
the required output is as follow :
{
"Aps": [
{
"type": "opo",
"price_min": 7,
"price_max":10,
"app_time": 0,
"arguments": {
"prices": 15,
"apps_num": "112"
},
"attributes": {
"name":"user1",
"priority":"100"
}
},
{
"type": "opo",
"price_min": 7,
"price_max":10,
"app_time": 0,
"arguments": {
"prices": 15,
"apps_num": "112"
},
"attributes": {
"name":"user2",
"priority":"90"
}
},
{
"type": "opo",
"price_min": 7,
"price_max":10,
"app_time": 0,
"arguments": {
"prices": 15,
"apps_num": "112"
},
"attributes": {
"name":"user2",
"priority":"80"
}
},
..............
}
答案1
得分: 2
我用json和copy模块编写了一小段代码。
json用于读取和写入json文件。
copy是因为我在处理引用变量时遇到了一些问题,可以参考copy的文档;如果我更改了'temp'字典,它会影响'file'字典中的所有出现。
import json
import copy
repeats = 100
file = json.loads(open('file.json', 'r').read())
temp1 = json.loads(open('file.json', 'r').read())['Aps'][0]
for repeat in range(repeats):
temp = copy.deepcopy(temp1)
temp['attributes']['name'] = f"user{repeat + 2}"
temp['attributes']['priority'] = f"{repeat*10+100 - repeat*20}"
file['Aps'].append(temp)
temp1 = copy.deepcopy(temp)
json.dump(file, open('file1.json', 'w'), indent=4)
英文:
I made a little code here using json and copy module
json for reading and writing json files
copy because I had some trouble with reference variables see documentation for copy; if I changed 'temp' dict then it would affect all occurrences in 'file' dict
import json
import copy
repeats = 100
file = json.loads(open('file.json', 'r').read())
temp1 = json.loads(open('file.json', 'r').read())['Aps'][0]
for repeat in range(repeats):
temp = copy.deepcopy(temp1)
temp['attributes']['name'] = f"user{repeat + 2}"
temp['attributes']['priority'] = f"{repeat*10+100 - repeat*20}"
file['Aps'].append(temp)
temp1 = copy.deepcopy(temp)
json.dump(file, open('file1.json', 'w'), indent=4)
答案2
得分: 1
你应该首先将你的JSON文件转换为一个Python对象(字典):
import json
file = open('sample.json')
data = json.load(file)
file.close()
现在你可以对你的Aps
列表执行操作,比如将第一个对象追加到列表中100次:
for dups in range(100):
data['Aps'].append(data['Aps'][0])
然后,你可以再次将你的字典保存到JSON文件中:
with open("sample.json", "w") as outputfile:
json.dump(data, outputfile)
英文:
You should first convert your json file to a python object (dict):
import json
file = open('sample.json')
data = json.load(file)
file.close()
Now you can do stuff with your Aps
list, like appending the first object 100 times to your list.
for dups in range(100):
data['Aps'].append(data['Aps'][0])
Then you save your dict to a json file again:
with open("sample.json", "w") as outputfile:
json.dump(data, outputfile)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论