python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法
如下所示:
#-*- coding: UTF-8 -*-
import json
import pymysql
import os
import sys
# 數(shù)據(jù)類型
# {
# "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數(shù)據(jù)提取轉(zhuǎn)化為txt的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 批量讀取文件中指定字符的實(shí)現(xiàn)
這篇文章主要介紹了Python 批量讀取文件中指定字符的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
python 用遞歸實(shí)現(xiàn)通用爬蟲解析器
這篇文章主要介紹了python 用遞歸實(shí)現(xiàn)通用爬蟲解析器的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
Python中的?Numpy?數(shù)組形狀改變及索引切片
這篇文章主要介紹了Python中的?Numpy?數(shù)組形狀改變及索引切片,Numpy提供了一個(gè)reshape()方法,它可以改變數(shù)組的形狀,返回一個(gè)新的數(shù)組,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章2022-05-05
Eclipse中Python開發(fā)環(huán)境搭建簡(jiǎn)單教程
這篇文章主要為大家分享了Eclipse中Python開發(fā)環(huán)境搭建簡(jiǎn)單教程,步驟簡(jiǎn)潔,一目了然,可以幫助大家快速搭建python開發(fā)環(huán)境,感興趣的小伙伴們可以參考一下2016-03-03
python數(shù)據(jù)寫入列表并導(dǎo)出折線圖
這篇文章主要介紹了python數(shù)據(jù)寫入列表并導(dǎo)出折線圖,文章以舉例展開對(duì)文章主題的介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-01-01
Pycharm虛擬環(huán)境pip時(shí)報(bào)錯(cuò):no?suchoption:--bulid-dir的解決辦法
最近在工作中遇到了些問題,想著大家應(yīng)該也會(huì)遇到過,所以給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Pycharm虛擬環(huán)境pip時(shí)報(bào)錯(cuò):no?suchoption:?--bulid-dir的解決辦法,需要的朋友可以參考下2022-07-07
使用Python操作Redis所有數(shù)據(jù)類型的方法
當(dāng)今互聯(lián)網(wǎng)時(shí)代,數(shù)據(jù)處理已經(jīng)成為了一個(gè)非常重要的任務(wù),而Redis作為一款高性能的NoSQL數(shù)據(jù)庫,越來越受到了廣大開發(fā)者的喜愛,本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級(jí)用法,需要的朋友可以參考下2023-11-11
django rest framework vue 實(shí)現(xiàn)用戶登錄詳解
這篇文章主要介紹了django rest framework vue 實(shí)現(xiàn)用戶登錄詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵
這篇文章主要介紹了pycharm 實(shí)現(xiàn)復(fù)制一行的快捷鍵,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01

