Python遍历文件夹 处理json文件
来源:互联网 发布:淘宝有名片设计吗 编辑:程序博客网 时间:2024/05/28 06:05
有两种做法:os.walk()、pathlib库,个人感觉pathlib库的path.glob用来匹配文件比较简单。
下面是第二种做法的实例(第一种做法百度有很多文章):
from pathlib import Pathimport jsonanalysis_root_dir = "D:\\analysis_data\json_file"store_result="D:\\analysis_data\\analysis_result\\dependency.csv"def parse_dir(root_dir): path = Path(root_dir) all_json_file = list(path.glob('**/*.json')) parse_result = [] for json_file in all_json_file: # 获取所在目录的名称 service_name = json_file.parent.stem with json_file.open() as f: json_result = json.load(f) json_result["service_name"] = service_name parse_result.append(json_result) return parse_resultdef write_result_in_file(write_path , write_content): with open(write_path,'w') as f: f.writelines("service_name,action,method,url\n") for dict_content in write_content: url = dict_content['url'] method = dict_content['method'] action = dict_content['action'] service_name = dict_content['service_name'] f.writelines(service_name + ","+ action+","+method + ","+ url+"\n")def main(): print("main begin...") parse_result = parse_dir(analysis_root_dir) print(parse_result) write_result_in_file(store_result,parse_result) print("main finished...")if __name__ == '__main__': main()
运行结果
main begin...[{'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService01'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService02'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService03'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService04'}, {'url': '/rest/webservice/v1/dosomthing', 'method': 'post', 'action': 'create', 'service_name': 'WebSubService05'}]main finished...
目录结构
json file内容
{"url":"/rest/webservice/v1/dosomthing","method":"post","action":"create"}
阅读全文
0 0
- Python遍历文件夹 处理json文件
- python 遍历文件夹 文件
- python 遍历文件夹及文件
- python-遍历文件夹与递归遍历文件
- python 处理json文件
- 【Python】Python处理Json文件
- python 文件和文件夹处理
- python 处理文件及文件夹
- [python]文件和文件夹处理
- java遍历文件夹读取文件进行处理
- java文件处理 之 遍历文件夹
- Python-遍历目标文件夹下所有文件
- python递归遍历文件夹检查文件
- Python文件操作及文件夹遍历
- Python 遍历文件夹中的指定类型文件
- Python 遍历文件夹中的指定类型文件
- 【Python】遍历文件夹,并命名文件
- 文件夹下的文件遍历-python
- Java 邮件发送功能
- docker设置centos容器支持
- bzoj2301 莫比乌斯反演
- RMI(Remote method invoke)详解
- angularjs中基本功能用法
- Python遍历文件夹 处理json文件
- 初窥Linux 之 我最常用的20条命令
- HDU 4622 Reincarnation(后缀自动机)
- 2017百度之星初赛第一场题解
- 2017.08.12小结
- 区间DP的主要思想
- Light oj 1138
- (模拟)2017"百度之星"程序设计大赛
- 在windows下黑窗口中操作数据库