【问题标题】:Create txt file of specified objects from JSON从 JSON 创建指定对象的 txt 文件
【发布时间】:2015-09-10 19:36:22
【问题描述】:

我已经通过 json 为服务报告创建了一个 html 文件,它可以根据需要工作。

#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' 服务的 txt 文件。 我可以创建 txt 文件,但我似乎无法只打印我需要的特定服务...... 想法?

【问题讨论】:

  • 什么是“我需要的特定服务......”?它们是如何识别的?
  • 请向我们展示您试图解决问题的代码,以便我们可以复制您的问题。它向我们展示了您对问题的理解,您应该比任何人都更了解。
  • 我只需要从打印到 txt 文件的 html 文件中列为“已停止”的服务。
  • 这是我的代码,不包括令牌信息
  • 哪个 json 部分表示它们“已停止”? i["status"]["realTimeState"] 也许?如果您编写了该代码,请不要让我们猜测...

标签: python json service


【解决方案1】:

你应该只维护一个停止服务的列表,在创建你的 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"

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 2015-09-30
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2017-04-21
    • 2018-04-11
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多