当前位置: 移动技术网 > IT编程>脚本编程>Python > python 将json数据提取转化为txt的方法

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

2018年11月05日  | 移动技术网IT编程  | 我要评论

星穹战马,内科病例分析题,送给我的朋友们伴奏

如下所示:

#-*- 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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网