加上「;条目“;使用Python创建JSON文件
我需要用python修改一个JSON文件。在我第一次使用python(和JSON)时,我读了一些关于它的文章,但没有完全理解它 我设法将JSON作为某种数组(或列表?)导入python JSON如下所示:加上「;条目“;使用Python创建JSON文件,python,json,python-2.7,Python,Json,Python 2.7,我需要用python修改一个JSON文件。在我第一次使用python(和JSON)时,我读了一些关于它的文章,但没有完全理解它 我设法将JSON作为某种数组(或列表?)导入python JSON如下所示: { "sources":[{ "id":100012630, "name":"Activity Login Page", "category":"NAM/Activity", "automaticDateParsing":true, "multili
{
"sources":[{
"id":100012630,
"name":"Activity Login Page",
"category":"NAM/Activity",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":true,
"timeZone":"Europe/Brussels",
"filters":[],
"cutoffTimestamp":1414364400000,
"encoding":"UTF-8",
"pathExpression":"C:\\NamLogs\\nam-login-page.log*",
"blacklist":[],
"sourceType":"LocalFile",
"alive":true
},{
"id":100001824,
"name":"localWinEvent",
"category":"NAM/OS/EventLog",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":1409090400000,
"encoding":"UTF-8",
"logNames":["Security","Application","System","Others"],
"sourceType":"LocalWindowsEventLog",
"alive":true
},{
"id":100001830,
"name":"localWinPerf",
"category":"NAM/OS/Perf",
"automaticDateParsing":false,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":0,
"encoding":"UTF-8",
"interval":60000,
"wmiQueries":[{
"name":"NAMID Service",
"query":"SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE Name = 'tomcat7'"
},{
"name":"CPU",
"query":"select * from Win32_PerfFormattedData_PerfOS_Processor"
},{
"name":"Logical Disk",
"query":"select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
},{
"name":"Physical Disk",
"query":"select * from Win32_PerfFormattedData_PerfDisk_PhysicalDisk"
},{
"name":"Memory",
"query":"select * from Win32_PerfFormattedData_PerfOS_Memory"
},{
"name":"Network",
"query":"select * from Win32_PerfFormattedData_Tcpip_NetworkInterface"
}],
"sourceType":"LocalWindowsPerfMon",
"alive":true
},
{
"sources":[{
"id":100012630,
"name":"Activity Login Page",
"category":"NAM/Activity",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":true,
"timeZone":"Europe/Brussels",
"filters":[],
"cutoffTimestamp":1414364400000,
"encoding":"UTF-8",
"pathExpression":"C:\\NamLogs\\nam-login-page.log*",
"blacklist":[],
"sourceType":"LocalFile",
"alive":true,
"collectorName":"Collector2910"
},{
"id":100001824,
"name":"localWinEvent",
"category":"NAM/OS/EventLog",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":1409090400000,
"encoding":"UTF-8",
"logNames":["Security","Application","System","Others"],
"sourceType":"LocalWindowsEventLog",
"alive":true,
"collectorName":"Collector2910"
},{.....
现在,当我得到数百个这样的文件时,我在整个目录中写了一个foreach:
for filename in os.listdir('./json/'):
with open('./json/'+filename) as data_file:
sources = json.load(data_file)
现在我需要类似于sources中的foreach源的东西,它向每个源(比如collectorName=fileName)添加一行(或者一个条目,或者JSON中的任何“行”),然后用新文件覆盖旧文件
JSON将如下所示:
{
"sources":[{
"id":100012630,
"name":"Activity Login Page",
"category":"NAM/Activity",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":true,
"timeZone":"Europe/Brussels",
"filters":[],
"cutoffTimestamp":1414364400000,
"encoding":"UTF-8",
"pathExpression":"C:\\NamLogs\\nam-login-page.log*",
"blacklist":[],
"sourceType":"LocalFile",
"alive":true
},{
"id":100001824,
"name":"localWinEvent",
"category":"NAM/OS/EventLog",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":1409090400000,
"encoding":"UTF-8",
"logNames":["Security","Application","System","Others"],
"sourceType":"LocalWindowsEventLog",
"alive":true
},{
"id":100001830,
"name":"localWinPerf",
"category":"NAM/OS/Perf",
"automaticDateParsing":false,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":0,
"encoding":"UTF-8",
"interval":60000,
"wmiQueries":[{
"name":"NAMID Service",
"query":"SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE Name = 'tomcat7'"
},{
"name":"CPU",
"query":"select * from Win32_PerfFormattedData_PerfOS_Processor"
},{
"name":"Logical Disk",
"query":"select * from Win32_PerfFormattedData_PerfDisk_LogicalDisk"
},{
"name":"Physical Disk",
"query":"select * from Win32_PerfFormattedData_PerfDisk_PhysicalDisk"
},{
"name":"Memory",
"query":"select * from Win32_PerfFormattedData_PerfOS_Memory"
},{
"name":"Network",
"query":"select * from Win32_PerfFormattedData_Tcpip_NetworkInterface"
}],
"sourceType":"LocalWindowsPerfMon",
"alive":true
},
{
"sources":[{
"id":100012630,
"name":"Activity Login Page",
"category":"NAM/Activity",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":true,
"timeZone":"Europe/Brussels",
"filters":[],
"cutoffTimestamp":1414364400000,
"encoding":"UTF-8",
"pathExpression":"C:\\NamLogs\\nam-login-page.log*",
"blacklist":[],
"sourceType":"LocalFile",
"alive":true,
"collectorName":"Collector2910"
},{
"id":100001824,
"name":"localWinEvent",
"category":"NAM/OS/EventLog",
"automaticDateParsing":true,
"multilineProcessingEnabled":false,
"useAutolineMatching":false,
"forceTimeZone":false,
"filters":[],
"cutoffTimestamp":1409090400000,
"encoding":"UTF-8",
"logNames":["Security","Application","System","Others"],
"sourceType":"LocalWindowsEventLog",
"alive":true,
"collectorName":"Collector2910"
},{.....
我希望我能解释我的问题,如果有人能帮助我,我会很高兴(即使有完全不同的解决方案)
提前谢谢
Michael,这里有一种方法:
for filename in os.listdir('./json/'):
sources = None
with open('./json/'+filename) as data_file:
sources = json.load(data_file)
sourcelist = sources['sources']
for i, s in enumerate(sourcelist):
sources['sources'][i]['collectorName'] = 'Collector' + str(i)
with open('./json/'+filename, 'w') as data_file:
data_file.write(json.dumps(sources))
为什么
sources['sources'][i]
当您将记录作为s
提供时?当你有json.dump(obj,file)
时,为什么要data\u file.write(json.dumps(sources))
?1)我不喜欢修改我正在迭代的列表。2) 它们是等价的,json.dump方法做的事情基本相同。只有在向列表中添加或从列表中删除项目时,修改正在迭代的列表才是危险的,而不是在修改列表中已有的项目时——在当前情况下,sources['sources'][i]是s==True
——两个名称都指向同一个对象。2.是的,但第二个问题是要编写的代码更少;)在这次尝试中,我还遇到了:ValueError:无法解码JSON对象。这是JSON.loads和JSON.load的问题吗?