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

2024-10-02 00:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经有密码了

#!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()  

但这段代码只是读一行然后把它分开。 实际上,我的霍尼德.txt" 我的目标是分开所有的线。在

如何拆分所有行并将其保存为“测试规则"? 在


Tags: testenvtxt密码closepyhtonreadlinebin
1条回答
网友
1楼 · 发布于 2024-10-02 00:31:47

您需要循环输入行;现在只调用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。在

相关问题 更多 >

    热门问题