【发布时间】:2014-07-29 23:18:43
【问题描述】:
我正在尝试读取txt 文件的每一行并打印出不同文件中的每一行。假设,我有一个包含如下文本的文件:
How are you? I am good.
Wow, that's great.
This is a text file.
......
现在,我希望filename1.txt 有以下内容:
How are you? I am good.
filename2.txt 拥有:
Wow, that's great.
等等。
我的代码是:
#! /usr/bin/Python
for i in range(1,4): // this range should increase with number of lines
with open('testdata.txt', 'r') as input:
with open('filename%i.txt' %i, 'w') as output:
for line in input:
output.write(line)
我得到的是,所有文件都包含文件的所有行。如上所述,我希望每个文件只有 1 行。
【问题讨论】: