Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

printing - Python output is different than what I need

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

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.

Thanks is advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you can change the script, do this:

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

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

This will remove NXX from its output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...