读取目录 json 合并
然后去重另保存
input
读取目录所有json
保存文件
/storage/emulated/0/下载/Document/
/storage/emulated/0/
/storage/emulated/0/0_培音/z
# 机器人的回答
import os
import json
def merge_json_files(directory_path, output_file_path):
all_data = []
# 读取目录中的所有JSON文件
for file_name in os.listdir(directory_path):
if file_name.endswith('.json'):
file_path = os.path.join(directory_path, file_name)
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
all_data.extend(data)
# 去重数据
unique_data = []
unique_set = set()
for item in all_data:
json_str = json.dumps(item, ensure_ascii=False)
if json_str not in unique_set:
unique_data.append(item)
unique_set.add(json_str)
# 保存合并后的文件
with open(output_file_path, 'w', encoding='utf-8') as f:
json.dump(unique_data, f, ensure_ascii=False)
# 设置输入目录和输出文件路径
input_directory = '/storage/emulated/0/下载/Document/'
output_file = '/storage/emulated/0/output.json'
# 调用函数进行合并和保存
merge_json_files(input_directory, output_file)
... if file_name.endswith('.json'):
... file_path = os.path.join(directory_path, file_name)
... with open(file_path, 'r', encoding='utf-8') as f:
... data = json.load(f)
... all_data.extend(data)
...
... # 去重数据
... unique_data = [dict(t) for t in {tuple(d.items()) for d in all_data}]
...
... # 保存合并后的文件
... with open(output_file_path, 'w', encoding='utf-8') as f:
... json.dump(unique_data, f, ensure_ascii=False)
...
>>> # 设置输入目录和输出文件路径
>>> input_directory = '/storage/emulated/0/下载/Document/'
>>> output_file = '/storage/emulated/0/output.json'
>>>
>>> # 调用函数进行合并和保存
>>> merge_json_files(input_directory, output_file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 13, in merge_json_files
File "<stdin>", line 13, in <setcomp>
AttributeError: 'str' object has no attribute 'items'
>>>
具体有多少错误,不太确定。
但是你的错误信息可能是提示你:unique_data = [dict(t) for t in {tuple(d.items()) for d in all_data}]4
这边出现的错误,你有必要看看你的all_data里的数据,理论上这个列表里的每一项应该是dict类型,但是看报错信息,似乎不是如此。这里可能包含了str类型
利用递归获取到json文件中的指定内容后,我需要做的是统计该关键字在本json文件中一共出现的数量。
思路:由于在上述递归过程中,得到的指定关键字对应的内容我们是用矩阵来存储的,于是,我们可以使用统计函数len()来统计其内容的个数,则是对应的关键字在json文件中出现的次数。
关键代码为:
number_count = len(list_shape)
整个程序的代码为:
import json
import os
# 处理嵌套json文件中指定关键字
# 处理字典值
def get_target_value(key, dic, tmp_list):
"""
:param key: 目标key值
:param dic: JSON数据
:param tmp_list: 用于存储获取的数据
:return: list
"""
if not isinstance(dic, dict) or not isinstance(tmp_list, list): # 对传入数据进行格式校验
return 'argv[1] not an dict or argv[-1] not an list '
if key in dic.keys():
tmp_list.append(dic[key]) # 传入数据存在则存入tmp_list
for value in dic.values(): # 传入数据不符合则对其value值进行遍历
if isinstance(value, dict):
get_target_value(key, value, tmp_list) # 传入数据的value值是字典,则直接调用自身
elif isinstance(value, (list, tuple)):
_get_value(key, value, tmp_list) # 传入数据的value值是列表或者元组,则调用_get_value
return tmp_list
# 处理元组或列表值
def _get_value(key, val, tmp_list):
for val_ in val:
if isinstance(val_, dict):
get_target_value(key, val_, tmp_list) # 传入数据的value值是字典,则调用get_target_value
elif isinstance(val_, (list, tuple)):
_get_value(key, val_, tmp_list)
# 处理单个json文件
# 修改json文件所在路径
def single_file_main():
path = "json_files/self_val_person_day.json"
with open(path, 'r') as load_f:
json_data = json.load(load_f) # json files to dict:json_data
number_shape = len(json_data.keys()) # json.key(): get all the images of one file(get number of images)
# data = list(json_data.keys()) # 可以索引
print(type(json_data))
print("number:", number_shape)
list_shape = get_target_value('shape_attributes', json_data, [])
number_count = len(list_shape)
print("shape_attributes:", list_shape)
print("numbers:", number_count)
# 批量处理指定文件夹下多个json文件
# 修改json文件所在的文件夹路径
def main():
file_list = os.listdir(r"./json_files")
print(file_list)
for filename in file_list:
with open(r"./json_files/"+filename, 'r') as load_f:
json_data = json.load(load_f) # json files to dict:json_data
list_shape = get_target_value('shape_attributes', json_data, []) # 利用json文件中的shape_attributes关键字可对应圈出的框的数量
number_count = len(list_shape)
print("file:", filename)
print("numbers:", number_count)
if __name__ == '__main__':
main()
得到的结果为:
/home/wenhaolun/anaconda3/bin/python /home/wenhaolun/PycharmProjects/pythonProject2/read_json.py
['三产(2).json', '三产(1).json', '四产(3).json', '四产(2).json', '车牌数据集-待标注.json', '三产(3).json', 'self_val_person_day.json', 'car,.json', '白天室外车牌.json', '四产(1).json', '同一车牌不同角度.json', 'self_val_person_night.json']
file: 三产(2).json
numbers: 309
file: 三产(1).json
numbers: 1178
file: 四产(3).json
numbers: 647
file: 四产(2).json
numbers: 1360