python将每个列表保存到某个输出文件中
我已经有了这个代码python将每个列表保存到某个输出文件中,python,file-io,Python,File Io,我已经有了这个代码 #!usr.bin/env python with open('honeyd.txt', 'r') as infile, open ('test.rule', 'w') as outfile: for line in infile: outfile.write('alert {} {} -> {} {}\n'.format(*line.split())) 此代码用于拆分所有行并将其保存到文件中 我的目标是分割所有行并将其保存到一些文件中,就
#!usr.bin/env python
with open('honeyd.txt', 'r') as infile, open ('test.rule', 'w') as outfile:
for line in infile:
outfile.write('alert {} {} -> {} {}\n'.format(*line.split()))
此代码用于拆分所有行并将其保存到文件中
我的目标是分割所有行并将其保存到一些文件中,就像我在honeyd.txt中的行一样多。一行对应一个输出文件。如果我有3行,那么每一行都会保存在一个输出文件中。所以我有3个输出文件。如果我有10行,那么每行都会保存在一个输出文件中。所以我有10个输出文件
有人能帮忙吗 假设您对文件名的顺序编号没有问题:
with open('honeyd.txt', 'r') as infile:
for index, line in enumerate(infile, 1):
with open('test.rule{}'.format(index), 'w') as outfile:
outfile.write('alert {} {} -> {} {}\n'.format(*line.split()))
这将创建名为test.rule1
、test.rule2
等的文件。请尝试以下操作:
with open('honeyd.txt') as f:
lines = [line.strip().split() for line in f] # a list of lists
for i in range(len(lines)):
with open('test_{}.rule'.format(i), 'w') as f2:
f2.write("alert {} {} -> {} {}\n".format(*lines[i]))