В Python 2. +:
ВХОД:
more original_file.json data_file
::::::::::::::
original_file.json
::::::::::::::
{
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "ENTRY1",
"auto": true,
"contexts": [],
"responses": []
}
::::::::::::::
data_file
::::::::::::::
AAA11
BBB12
CCC13
Python-скрипт:
import json
#open the original json file
with open('original_file.json') as handle:
#create a dict based on the json content
dictdump = json.loads(handle.read())
#file counter
i = 1
#open the data file
f = open("data_file", "r")
#get all lines of the data file
lines = f.read().splitlines()
#close it
f.close()
#for each line of the data file
for line in lines:
#change the value of the json name element by the current line
dictdump['name'] = line
#open newfileX
o = open("newfile" + str(i),'w')
#dump the content of modified json
json.dump(dictdump,o)
#close the file
o.close()
#increase the counter value
i += 1
output:
more newfile*
::::::::::::::
newfile1
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "AAA11"}
::::::::::::::
newfile2
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "BBB12"}
::::::::::::::
newfile3
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "CCC13"}
Если вам нужно вывести дамп json по вертикали, вы можете адаптировать строку: json.dump(dictdump, o)
в json.dump(dictdump, o, indent=4)
.Это приведет к:
more newfile*
::::::::::::::
newfile1
::::::::::::::
{
"contexts": [],
"auto": true,
"responses": [],
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "AAA11"
}
::::::::::::::
newfile2
::::::::::::::
{
"contexts": [],
"auto": true,
"responses": [],
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "BBB12"
}
::::::::::::::
newfile3
::::::::::::::
{
"contexts": [],
"auto": true,
"responses": [],
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "CCC13"
}
DOC: https://docs.python.org/2/library/json.html
Более новая версия для сохранения того же порядка, что и для ввода:
import json
from collections import OrderedDict
#open the original json file
with open('original_file.json') as handle:
#create a dict based on the json content
dictdump = json.loads(handle.read(), object_pairs_hook=OrderedDict)
#file counter
i = 1
#open the data file
f = open("data_file", "r")
#get all lines of the data file
lines = f.read().splitlines()
#close it
f.close()
#for each line of the data file
for line in lines:
#change the value of the json name element by the current line
dictdump['name'] = line
#open newfileX
o = open("newfile" + str(i),'w')
#dump the content of modified json
json.dump(dictdump, o, indent=4)
#close the file
o.close()
#increase the counter value
i += 1
Вывод:
more newfile*
::::::::::::::
newfile1
::::::::::::::
{
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "AAA11",
"auto": true,
"contexts": [],
"responses": []
}
::::::::::::::
newfile2
::::::::::::::
{
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "BBB12",
"auto": true,
"contexts": [],
"responses": []
}
::::::::::::::
newfile3
::::::::::::::
{
"id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
"name": "CCC13",
"auto": true,
"contexts": [],
"responses": []
}