【发布时间】:2015-05-28 11:06:36
【问题描述】:
我需要用 python 修改一个 JSON 文件。由于我是第一次使用 python(和 JSON),我阅读了一些关于它的文章,但没有完全理解它。
我设法将 JSON 作为某种数组(或列表?)导入 python。
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
},
现在,当我得到数百个这样的文件时,我在整个目录中编写了一个 foreach:
for filename in os.listdir('./json/'):
with open('./json/'+filename) as data_file:
sources = json.load(data_file)
现在我需要在源中再次使用类似 foreach 源的东西,它将一行(或一个条目或 JSON 中称为任何“行”的任何东西)添加到每个源(类似于 collectorName=fileName),然后覆盖旧的用新的文件。
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,
"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"
},{.....
我希望我能解释我的问题,如果有人可以帮助我(即使是完全不同的解决方案),我会很高兴。
提前致谢
迈克尔
【问题讨论】:
标签: python json python-2.7