如何使用 python 合并 json 文件中的相同对象
how to merge same object in json file using python
1.json 文件包含许多嗅探 WIFI 数据包,我想获取接收器和发射器的 mac 地址,可以在第一个 "wlan" 对象中找到 "wlan.ra"和 "wlan.sa"。 data[0]是第一个WIFI包。
Q1:
但是当我尝试在 json 加载后打印 wlan 的元素时,它只显示第二个 "wlan" 对象的元素,因此数据中没有 "wlan.ra" 和 "wlan.sa" .
with open('1.json','r') as json_data:
data = json.load(json_data)
a=data[0]
print a
Q2:
我的 json 文件中有两个 'wlan' 对象。如何将这两个 'wlan' 对象中的元素合并为一个 'wlan' 对象?
以下是我的代码,但它不起作用:
with open('1.json','r') as f:
data=json.load(f)
for i in data:
i['_source']['layers']['wlan'].update()
json 文件的屏幕截图:
'''
Created on 2017/10/3
@author: DD
'''
import os
def modify_jsonfile(jsonfile):
'''
replace wlan to wlan1/wlan2
'''
FILESUFFIX = '_new' # filename suffix
LBRACKET = '{' # json object delimiter
RBRACKET = '}'
INTERSETED = '"wlan"' # string to be replaced
nBrackets = 0 # stack to record object status
nextIndex = 1 # next index of wlan
with open(jsonfile, 'r') as fromJsonFile:
fields = os.path.splitext(jsonfile) # generate new filename
with open(fields[0] + FILESUFFIX + fields[1], 'w') as toJsonFile:
for line in fromJsonFile.readlines():
for ch in line: # record bracket
if ch == LBRACKET:
nBrackets += 1
elif ch == RBRACKET:
nBrackets -= 1
if nBrackets == 0:
nextIndex = 1
if (nextIndex == 1 or nextIndex == 2) and line.strip().find(INTERSETED) == 0: # replace string
line = line.replace(INTERSETED, INTERSETED[:-1] + str(nextIndex) + INTERSETED[-1])
nextIndex += 1
toJsonFile.write(line);
print 'done.'
if __name__ == '__main__':
jsonfile = r'C:\Users\DD\Desktop.json';
modify_jsonfile(jsonfile)
1.json 文件包含许多嗅探 WIFI 数据包,我想获取接收器和发射器的 mac 地址,可以在第一个 "wlan" 对象中找到 "wlan.ra"和 "wlan.sa"。 data[0]是第一个WIFI包。
Q1: 但是当我尝试在 json 加载后打印 wlan 的元素时,它只显示第二个 "wlan" 对象的元素,因此数据中没有 "wlan.ra" 和 "wlan.sa" .
with open('1.json','r') as json_data:
data = json.load(json_data)
a=data[0]
print a
Q2: 我的 json 文件中有两个 'wlan' 对象。如何将这两个 'wlan' 对象中的元素合并为一个 'wlan' 对象?
以下是我的代码,但它不起作用:
with open('1.json','r') as f:
data=json.load(f)
for i in data:
i['_source']['layers']['wlan'].update()
json 文件的屏幕截图:
'''
Created on 2017/10/3
@author: DD
'''
import os
def modify_jsonfile(jsonfile):
'''
replace wlan to wlan1/wlan2
'''
FILESUFFIX = '_new' # filename suffix
LBRACKET = '{' # json object delimiter
RBRACKET = '}'
INTERSETED = '"wlan"' # string to be replaced
nBrackets = 0 # stack to record object status
nextIndex = 1 # next index of wlan
with open(jsonfile, 'r') as fromJsonFile:
fields = os.path.splitext(jsonfile) # generate new filename
with open(fields[0] + FILESUFFIX + fields[1], 'w') as toJsonFile:
for line in fromJsonFile.readlines():
for ch in line: # record bracket
if ch == LBRACKET:
nBrackets += 1
elif ch == RBRACKET:
nBrackets -= 1
if nBrackets == 0:
nextIndex = 1
if (nextIndex == 1 or nextIndex == 2) and line.strip().find(INTERSETED) == 0: # replace string
line = line.replace(INTERSETED, INTERSETED[:-1] + str(nextIndex) + INTERSETED[-1])
nextIndex += 1
toJsonFile.write(line);
print 'done.'
if __name__ == '__main__':
jsonfile = r'C:\Users\DD\Desktop.json';
modify_jsonfile(jsonfile)