本文介绍了Python输出与我需要的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是python的新手,我需要您的帮助.

Hi guys I'm farely new to python and I need your help.

import sys, http.client

file = open('out.txt', 'w')
showlines = 50
npa = []

try:
     servername = sys.argv[1]
except:
    servername = 'localcallingguide.com'
server = http.client.HTTPConnection(servername)

for i in range(1000):
    if i < 10:
        npa.append('00' + str(i))
    elif i >= 10 and i < 100:
        npa.append('0' + str(i))
    else:
        npa.append(str(i))

for i in range(len(npa)):
    filename = '/lca_rcdist.php?npa1=503&nxx1=745&npa2=503&nxx2=' + npa[i]
    server.putrequest('GET', filename)
    server.putheader('Accept', 'text/html')
    server.endheaders()
    reply = server.getresponse()

    if reply.status != 200:
        print('Error sending request', reply.status, reply.reason)
    else:
        data = reply.readlines()
        reply.close()
        for line in data[:showlines]:
            cLine = line.decode('utf-8')
            if '"ofrom">N<' in cLine:
                file.write('NXX ,' + npa[i])

file.close

使用上面的脚本,我得到的输出为"NXX,503,NXX,203,依此类推,我需要从输出中隐藏NXX,任何想法如何在不删除脚本的NXX的情况下做到这一点.

With the above script, I get an output of "NXX, 503, NXX, 203, and so on, I need to hide the NXX from the output, any idea how to do that without removing the NXX for the script.

谢谢前进!

推荐答案

如果您可以更改脚本,请执行以下操作:

If you can change the script, do this:

file.write(',' + npa[i])

如果您不能更改此脚本,请执行以下操作:

如果您的脚本名为"example.py",则可以这样调用它:

If you can't change this script do this:

If your script is called "example.py", then you could invoke it like this:

python example.py ; sed -i s/NXX//g out.txt

这将从其输出中删除NXX.

这篇关于Python输出与我需要的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 01:39