python用于拆分所有行并保存在新的输出文件中

python用于拆分所有行并保存在新的输出文件中,python,string-split,Python,String Split,我已经有了这个代码 #!usr.bin/env pyhton asal = open("honeyd.txt") tujuan = open("test.rule", "W") satu = asal.readline() a = satu.split(); b = 'alert ' + a[0]+' ' + a[1] + ' -> ' + a[2]+' '+ a[3] c = str(b) tujuan.write(c) asal.close() tujuan.close() 但这

我已经有了这个代码

#!usr.bin/env pyhton
asal = open("honeyd.txt")
tujuan = open("test.rule", "W")
satu = asal.readline()
a = satu.split();
b = 'alert ' + a[0]+' ' + a[1] + ' -> ' + a[2]+' '+ a[3]
c = str(b)
tujuan.write(c)
asal.close()
tujuan.close()  
但这段代码只是读取一行并拆分它。 实际上,我的“honeyd.txt”中有3行 我的目标是分割所有的线


如何分割所有行并将其保存到“test.rule”中?

您需要在输入行上循环;现在您只需调用
readline
一次。最好直接在输入文件句柄上循环:

with open('honeyd.txt') as infile, open('test.rule', 'w') as outfile:
    for line in infile:
        outfile.write('alert {} {} -> {} {}'.format(*line.split())

还要注意
with
语句的使用,这使您不必手动调用
close

您能提供示例输入和所需输出吗?