Python遍历文件夹 处理json文件的方法

 更新时间:2019年01月22日 09:12:03   作者:Norton-Linux内核研究  
今天小编就为大家分享一篇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"
}

以上这篇Python遍历文件夹 处理json文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python openpyxl方法 zip函数用法及说明

    python openpyxl方法 zip函数用法及说明

    这篇文章主要介绍了python openpyxl方法 zip函数用法及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • python+matplotlib绘制饼图散点图实例代码

    python+matplotlib绘制饼图散点图实例代码

    这篇文章主要介绍了python+matplotlib绘制饼图散点图实例代码,本实例是官网的一个实例,不过也很简单,大家可以参考下
    2018-01-01
  • 浅析Python中常见数据脱敏技术应用与对比

    浅析Python中常见数据脱敏技术应用与对比

    数据脱敏通过对敏感数据进行转换,确保其在保护隐私的同时仍能用于开发,本文为大家整理了一些常见的数据脱敏技术,感兴趣的小伙伴可以了解下
    2025-02-02
  • 5分钟 Pipenv 上手指南

    5分钟 Pipenv 上手指南

    这篇文章主要介绍了5分钟 Pipenv 上手指南,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 使用PIL(Python-Imaging)反转图像的颜色方法

    使用PIL(Python-Imaging)反转图像的颜色方法

    今天小编就为大家分享一篇使用PIL(Python-Imaging)反转图像的颜色方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python安装whl文件过程图解

    Python安装whl文件过程图解

    这篇文章主要介绍了Python安装whl文件过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Python自动化测试工具Splinter简介和使用实例

    Python自动化测试工具Splinter简介和使用实例

    这篇文章主要介绍了Python自动化测试工具Splinter简介和使用实例,Splinter可以非常棒的模拟浏览器的行为,Splinter提供了丰富的API,可以获取页面的信息判断当前的行为所产生的结果
    2014-05-05
  • Python3之字符串比较_重写cmp函数方式

    Python3之字符串比较_重写cmp函数方式

    这篇文章主要介绍了Python3之字符串比较_重写cmp函数方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Python时间序列缺失值的处理方法(日期缺失填充)

    Python时间序列缺失值的处理方法(日期缺失填充)

    这篇文章主要给大家介绍了关于Python时间序列缺失值(日期缺失填充)的处理方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • 对Python中9种生成新对象的方法总结

    对Python中9种生成新对象的方法总结

    今天小编就为大家分享一篇对Python中9种生成新对象的方法总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05

最新评论