拆分数据并将其保存为文本文档

时间:2012-10-24 09:48:04

标签: python python-2.7

所以我有一份文本文件text_input 我想使用python将文本中的两个值拆分为两个单独的文本文档。                                                                             。

非常感谢你们为帮助对方所做的一切努力!

1 个答案:

答案 0 :(得分:2)

这应该这样做:

>>> with open('input.txt') as f1,open('output1.txt','w') as f2,open('output2.txt','w') as f3:
...     for line in f1:             #read data line by line
...         lin=line.split()        #split the line into ['19490622','14.0'] 
...         f2.write(lin[0]+'\n')   #write first part to output1
...         f3.write(lin[1]+'\n')   #write second part to output2