python 将json数据提取转化为txt的方法

yipeiwu_com6年前Python基础

如下所示:

#-*- coding: UTF-8 -*-
import json
import pymysql
import os
import sys
# 数据类型
# {
#  "name": "score.networkQuality",
#  "index": true,
#  "view": "app/views/score/networkQuality.tmpl.html",
#  "files": ["app/modules/controllers/score/networkQualityCtrl.js"]
# },
name = []
index = []
views = []
files = []
# "name": "monitored.monitored",
with open('C:\\Users\\sxf\\Desktop\\app-modules.json') as f:
 for line in f:
  try:
   line.index("name")
   # line = line.strip('\n')
   pos = line.index(':')
   name.append(line[pos+3:len(line)-3])
  except ValueError:
   pass
  try:
   line.index("true")
   # line = line.strip('\n')
   pos = line.index(':')
   index.append((line[pos+2:len(line)-2]))
  except ValueError:
   pass
  try:
   line.index("view")
   try:
    line.index("name")
   except ValueError:
    pos = line.index(':')
    views.append(line[pos + 3:len(line) - 3])
  except ValueError:
   pass
  # ['app/modules/controllers/monitored/monitoredCtrl.js","app/modules/services/resources/resourcesService.j']
  try:
   line.index("files")
   # line = line.strip('\n')
   pos = line.index(":")
   try:
    i=0
    str1 = ""
    str2 = ""
    str3 = ""
    pos_comma = line.index(",")
    str = line.split(',')
    comma_count = line.count(',')
    while i<=comma_count:
     if i == 0:
      # str[0] = str[0].strip('\n')
      str1 = str[0][pos+4:len(str[0])-1]
     elif i == 1 :
      if comma_count == 1:
       # str[1] = str[1].strip('\n')
       str2 = str1+','+str[1][1:len(str[1])-3]
      else:
       str2 = str1 + ',' + str[1][1:len(str[1]) - 1]
     elif i == 2 :
      # str[2] = str[2].strip('\n')
      str3 = str2+','+str[2][1:len(str[2])-3]
     else:
      pass
     i = i + 1
    if (comma_count == 1):
     files.append(str2)
    elif (comma_count == 2):
     files.append(str3)
   except ValueError:
    # line = line.strip('\n')
    files.append(line[pos+4:len(line)-3])
    pass
  except ValueError:
   pass
 if os.path.exists("C:\\Users\\sxf\\Desktop\data.txt"):
  os.remove("C:\\Users\\sxf\\Desktop\data.txt")
 write_file = open("C:\\Users\\sxf\\Desktop\data.txt","a+")
 j=0
 while j< len(name):
  str_info = name[j]+"\t"+index[j]+"\t"+views[j]+"\t"+files[j]+"\n"
  write_file.write(str_info)
  j = j + 1

以上这篇python 将json数据提取转化为txt的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python_LDA实现方法详解

LDA(Latent Dirichlet allocation)模型是一种常用而用途广泛地概率主题模型。其实现一般通过Variational inference和Gibbs Sampin...

python批量生成本地ip地址的方法

本文实例讲述了python批量生成本地ip地址的方法。分享给大家供大家参考。具体分析如下: 这段代码用于在本地计算机上生成本地ip地址绑定到网卡,生成的是一个bat的批处理文件,运行此批...

python在openstreetmap地图上绘制路线图的实现

python在openstreetmap地图上绘制路线图的实现

利用python进行经纬度轨迹展示 嘿!各位好久不见,距离第一次发博客已经过去两年多了,本人也从本科生变成了研究生,好了书归正传,最近在做一个关于航班滑行路径轨迹的项目,目的是将航班的经...

Python 将RGB图像转换为Pytho灰度图像的实例

问题: 我正尝试使用matplotlib读取RGB图像并将其转换为灰度。 在matlab中,我使用这个: img = rgb2gray(imread('image.png'));...

解决PyCharm同目录下导入模块会报错的问题

在PyCharm2017中同目录下import其他模块,会出现No model named ...的报错,但实际可以运行 这是因为PyCharm不会将当前文件目录自动加入source_p...