• Python读取json文件中的数据和统计行数【多测师】


    json源数据如下:
    {
     "name":"中国",
     "province":[
     {
           "name":"多测师",
           "citys":{
               "city":["上海多测师","上海多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师","深圳多测师","广州多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师","深圳多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师"]
           }
       }
     ]
    }
    #coding=utf-8
    """
    ===========================
    Author:多测师_王sir
    Time:2020/6/22 15:04
    Wechat:15367499889
    Company:上海多测师信息有限公司
    ===========================
    """
    
    import os
    
    class Json_Utils:
    
        def __init__(self,path):
            self.path = path
    
        def read_json(self):
            file = open(self.path,'r')
            all = file.readlines()
            for j in all:
                print j.strip('
    ')   #读取json中的文件内容
            num = 0
            for i in all:
                num+=1
            print 'json文件的行数为{0}行'.format(num)
    
    if __name__ == '__main__':
        read = Json_Utils(r'D:projectdcs2api_autox0x0.json')
        read.read_json()
    运行结果如下:
    
    
    {
     "name":"中国",
     "province":[
     {
           "name":"多测师",
           "citys":{
               "city":["上海多测师","上海多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师","深圳多测师","广州多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师","深圳多测师"]
           }
       },
       {
       "name":"多测师",
           "citys":{
               "city":["上海多测师"]
           }
       }
     ]
    }
    json文件的行数为29行
  • 相关阅读:
    MySQL-事务相关知识
    Linux脚本-自动ping网址列表
    洛谷 P2453 [SDOI2006]最短距离
    洛谷 P2915 [USACO08NOV]Mixed Up Cows G
    洛谷 P2473 [SCOI2008] 奖励关
    洛谷 P3391 【模板】文艺平衡树
    洛谷 P4146 序列终结者
    洛谷 P1486 [NOI2004] 郁闷的出纳员
    洛谷 P2596 [ZJOI2006]书架
    性能测试工具Jmeter02-安装配置
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/13178215.html
Copyright © 2020-2023  润新知