2015-09-10 2 views
-2

Я создал html-файл для отчета службы через json и работает по мере необходимости.Создать txt-файл указанных объектов из JSON

#Creates Html page 
f = open("Oceaneering_Server_Status.html", "w") 
f.write('''<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 

<style type=text/css> 
body{{ 
    background-color: #D9D8D5; 
}} 
table, th, td {{ 

    border: 2px solid black; 
    border-collapse: collapse; 
}} 
th {{ 
    text-align: center; 
}} 
td {{ 
    text-align: center; 
    contains("STARTED").css('color', 'red'); 

}} 
</style> 

<body> 
<h1>Oceaneering Server Status: </h1> 
<input type="text" id="search" placeholder="Type to search"> 
<p>Last updated: {time}<br>Services Running: XXXX</br><p id="row"></br></p> 
<table id="table" class="tablesorter" style="margin: 0px auto;" table class="sortable">> 
<caption>Last updated: XXXX Total Services: XXXX Services Stopped: XXX Services Running: XXX </caption> 
<col width="100"> 
    <tr> 
    <th>Service</th> 
    <th>Folder</th> 
    <th>Service URL</th> 
    <th>Configured State</th> 
    <th>Real Time State</th> 
    <th>Server Type</th> 
    </tr>'''.format(time=date)) 

for item in json_read["folders"]: 
     services_url = "https://www.ocsdev.oceaneering.com/arcgis/admin/services/" + item + "/report?f=pjson&token=" + token 
     t = [] 
     t.append(services_url) 
     print t 

     for i in t: 
      services_open = urllib.urlopen(services_url) 
      services_js = json.loads(services_open.read()) 
      #print services_js 


     #z = open("test.text", "w") 
     for i in services_js["reports"]: 
      f = open("Oceaneering_Server_Status.html", "a") 
      line1 = "<tr>" + "\n\t<td>" + i["instances"]["serviceName"] + "</td>\n" 
      serv_count = i["instances"]["serviceName"]+ "\n" 
      line2 = "\t<td>" + i["instances"]["folderName"] + "</td>\n" 
      line3 = '\t<td><a href= "https://www.ocsdev.oceaneering.com/arcgis/rest/services/' + i["instances"]["folderName"] +"/"+ i["instances"]["serviceName"] + "/MapServer?f=jsapi&token=" + token + '">' + i["instances"]["serviceName"] + "</a>" + "</td>\n" 
      line4 = "\t<td>" + i["status"]["configuredState"] + "</td>\n" 
      line5 = "\t<td>" + i["status"]["realTimeState"] + "</td>\n" 
      line6 = "\t<td>" + i["instances"]["type"] + "</td>\n" 
      f.write(line1 + line2 + line3 + line4 + line5 + line6) 
      #z.write(serv_count) 


#z.close() 

##with open('test.text') as b: 
## aws = len(b.readlines()) 
##print aws 


f.write('''</table> 

<script> 

$("td:contains('STARTED')").css("color", "green"); 
$("td:contains('STOPPED')").css("color", "red"); 



var $rows = $('#table tr'); 
$('#search').keyup(function() { 
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 

    $rows.show().filter(function() { 
     var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
     return !~text.indexOf(val); 
    }).hide(); 
}); 

var tableSize = "Total Services: " + $('#table tr').length; 
document.getElementById("row").innerHTML = tableSize; 

</script> 

</body> 
</html>''') 
f.close() 


print "completed" 

#_______________________________________________________________________________# 
#Create TXT file of Services 

import json 
with open('Oceaneering_Server_Status.txt', 'w') as outfile: 
    json.dump(json_read, outfile) 

print "completed" 

Я пытаюсь затем создать текстовый файл из «Stopped» услуг .. Я в состоянии создать текстовый файл, но я не могу печатать только определенные услуги, для которых Мне нужно ... Мысли?

+0

Какие «конкретные услуги, для которых мне нужно ... «? Как они идентифицируются? – jojonas

+0

, пожалуйста, покажите нам код того, что вы пытаетесь решить, чтобы решить вашу проблему, чтобы мы могли реплицировать вашу проблему. Это показывает нам, что вы понимаете в своей проблеме, которую вы должны понимать лучше всех. –

+0

Мне нужны только сервисы, перечисленные как «STOPPED» из html-файла, напечатанного в txt-файле. – CDG

ответ

0

Вы просто должны поддерживать список остановленных услуг, наполняя его при создании HTML-файла и демпинг только его содержимое в файл JSON:

#Creates Html page 
f = open("Oceaneering_Server_Status.html", "w") 
f.write('''<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 

<style type=text/css> 
body{{ 
    background-color: #D9D8D5; 
}} 
table, th, td {{ 

    border: 2px solid black; 
    border-collapse: collapse; 
}} 
th {{ 
    text-align: center; 
}} 
td {{ 
    text-align: center; 
    contains("STARTED").css('color', 'red'); 

}} 
</style> 

<body> 
<h1>Oceaneering Server Status: </h1> 
<input type="text" id="search" placeholder="Type to search"> 
<p>Last updated: {time}<br>Services Running: XXXX</br><p id="row"></br></p> 
<table id="table" class="tablesorter" style="margin: 0px auto;" table class="sortable">> 
<caption>Last updated: XXXX Total Services: XXXX Services Stopped: XXX Services Running: XXX </caption> 
<col width="100"> 
    <tr> 
    <th>Service</th> 
    <th>Folder</th> 
    <th>Service URL</th> 
    <th>Configured State</th> 
    <th>Real Time State</th> 
    <th>Server Type</th> 
    </tr>'''.format(time=date)) 

stopped = [] 
for item in json_read["folders"]: 
     services_url = "https://www.ocsdev.oceaneering.com/arcgis/admin/services/" + item + "/report?f=pjson&token=" + token 
     t = [] 
     t.append(services_url) 
     print t 

     for i in t: 
      services_open = urllib.urlopen(services_url) 
      services_js = json.loads(services_open.read()) 
      #print services_js 


     #z = open("test.text", "w") 
     for i in services_js["reports"]: 
      f = open("Oceaneering_Server_Status.html", "a") 
      line1 = "<tr>" + "\n\t<td>" + i["instances"]["serviceName"] + "</td>\n" 
      serv_count = i["instances"]["serviceName"]+ "\n" 
      line2 = "\t<td>" + i["instances"]["folderName"] + "</td>\n" 
      line3 = '\t<td><a href= "https://www.ocsdev.oceaneering.com/arcgis/rest/services/' + i["instances"]["folderName"] +"/"+ i["instances"]["serviceName"] + "/MapServer?f=jsapi&token=" + token + '">' + i["instances"]["serviceName"] + "</a>" + "</td>\n" 
      line4 = "\t<td>" + i["status"]["configuredState"] + "</td>\n" 
      line5 = "\t<td>" + i["status"]["realTimeState"] + "</td>\n" 
      line6 = "\t<td>" + i["instances"]["type"] + "</td>\n" 
      f.write(line1 + line2 + line3 + line4 + line5 + line6) 
      #z.write(serv_count) 

      if i["status"]["realTimeState"] == "STOPPED": 
       stopped.append(i) 


#z.close() 

##with open('test.text') as b: 
## aws = len(b.readlines()) 
##print aws 


f.write('''</table> 

<script> 

$("td:contains('STARTED')").css("color", "green"); 
$("td:contains('STOPPED')").css("color", "red"); 



var $rows = $('#table tr'); 
$('#search').keyup(function() { 
    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 

    $rows.show().filter(function() { 
     var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
     return !~text.indexOf(val); 
    }).hide(); 
}); 

var tableSize = "Total Services: " + $('#table tr').length; 
document.getElementById("row").innerHTML = tableSize; 

</script> 

</body> 
</html>''') 
f.close() 


print "completed" 

#_______________________________________________________________________________# 
#Create TXT file of Services 

import json 
with open('Oceaneering_Server_Status.txt', 'w') as outfile: 
    json.dump(stopped, outfile) 

print "completed" 
Смежные вопросы