2016-03-01 113 views
-7

这是一个文本文件。如何将文本文件分割为多个文本文件

Oh , yeah , we got this puppy . 
This was a good idea . 
Elliot looks a little green . 
No . 

并包含如此多的行。

我想将这个文件分割成两个文本文件,就像这种方式使用python或其他方式可以在linux中使用。

input.txt

Oh , yeah , we got this puppy . 
Elliot looks a little green . 

response.txt

This was a good idea . 
No . 

所以,我想获得两个文本文件;一个有奇数行,另一个有偶数行。 我该怎么办?

+4

你能告诉你的企图在这个好吗? – idjaw

回答

-1

尝试这样的:

with open("your_file") as f, open("input.txt", "w") as inp, open("output.txt", "w") as out: 
    for i,line in enumerate(f): 
     if (i+1)%2 == 0: 
      out.write(line) 
     else: 
      inp.write(line)