for i in range(len(npa)):
filename = '/lca_rcdist.php?npa1=503&nxx1=745&npa2=503&nxx2=' + npa[i]
reply = urllib.request.urlopen(servername + filename)
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:
print('NXX ,' + npa[i])
下面的输出是NXX,269NXX,298NXX,300NXX
等等,有没有办法加上if和else语句,让输出的前面不带逗号和NXX第一次进入?示例:269NXX、298NXX
?我对此很陌生,但仍在为此类脚本中的 if 和 else 语句而苦苦挣扎。关于如何使用 if 更改输出的任何 ifo,否则 statemnet 将被应用。
最佳答案
创建一个列表,然后使用 str.join()
:
result = [] # NEW
for i in range(len(npa)):
... # blah, blah, same as your example, until
if '"ofrom">N<' in cLine:
result.append(npa[i]) # CHANGED
print (','.join(result)) #NEW
关于python - if,else语句输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18990982/