Python遍历文件夹 处理json文件的方法
作者:Norton-Linux内核研究 发布时间:2022-02-19 16:08:40
标签:Python,遍历,文件夹,json
有两种做法:os.walk()、pathlib库,个人感觉pathlib库的path.glob用来匹配文件比较简单。
下面是第二种做法的实例(第一种做法百度有很多文章):
from pathlib import Path
import json
analysis_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_result
def 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"
}
来源:https://blog.csdn.net/xzongyuan/article/details/77121392


猜你喜欢
- 前言判断文件是否存在在实际应用中用的非常多,下面我们来归纳一下检查文件、文件夹是否存在的各种操作一.检查文件夹/文件是否存在1. os.pa
- function flushDataTree() { dataset_mainMenuTemp.setShowLoadingTip(true
- 用于匹配的正则表达式为 :([1-9]\d*\.?\d*)|(0\.\d*[1-9])([1-9] :匹配1~9的数字;\d :匹配数字,包
- 本文实例为大家分享了Vue实现步骤条效果的具体代码,供大家参考,具体内容如下步骤总数和初始选择步骤 均可自定义设置,每个步骤title和de
- IE独有属性AlphaImageLoader用于修正7.0以下版本中显示PNG图片的半透明效果。这个滤镜的问题在于浏览器加载图片时它会终止内
- SQL Server 2005开始,我们可以直接通过CTE来支持递归查询,CTE即公用表表达式公用表表达式(CTE),是一个在查询中定义的临
- 本文实例讲述了Python根据指定日期计算后n天,前n天是哪一天的方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8
- UEditor效果图一、简介UEditor是一个开源免费的编辑器,由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,
- 1从SQLServer导出数据 执行BCP: bcp "..." queryout "F:\test.txt&
- jenkins是什么? Jenkins是一个开
- 一、概述:用来描述或者匹配一系列符合某个语句规则的字符串二、单个符号1、英文句点.符号:匹配单个任意字符。表达式t.o 可以匹配:tno,t
- argparse 是 python 的一个命令行解析包,可根据需要编写高可读性的程序。网上的许多教程较为冗长和散漫,没有达到精练好掌握的目的
- 1.定时器Timer定时器源码实现,和自定义一个线程方式一样,都是继承Thread类,重写了run()方法,只是实现的功能是延时执行一个函数
- 最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:1、scatter函数原型2、其
- 思路:遍历文件夹下面的文件夹如果文件夹名称等于".svn",则修改文件夹的属性(因为".svn"的文
- 在Python个人博客程序开发实例框架设计中,我们已经完成了 数据库设计、数据准备、模板架构、表单设计、视图函数设计、电子邮件支持 等总体设
- mysql在查询上千万级数据的时候,通过索引可以解决大部分查询优化问题。但是在处理上亿数据的时候,索引就不那么友好了。数据表(日志)是这样的
- 目录1.celery异步消息队列介绍celery应用举例Celery有以下优点Celery 特性2.工作原理 *****Celery 扮演生
- #coding:utf-8 #批量修改文件名 import os import re import datetime re_st = r
- 本文实例讲述了Django rest framework工具包简单用法。分享给大家供大家参考,具体如下:Django rest framew