Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python将每个列表保存到某个输出文件中_Python_File Io - Fatal编程技术网

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]))