2016-03-01 111 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)