作者painkiller (肚子饿~)
看板Python
标题[问题] 如何读取txt档中的资料并写入另一个txt档?
时间Wed Jun 23 10:45:05 2010
我是python新手
对於基本的资料处理还不是很熟悉
如果 1.txt 档中的资料形式为:
1 2 3
4 5 6
7 8 9
我只想要抓取第二到第三列的数字, 跳过空行
存到2.txt里为
1 3
4 6
7 9
基本上我只能写出
in_f=open('1.txt','r')
for line in in_f:
s1=line.split()
s2=str(s1[1:2])
print s2 #确认取得所需资料
out_f.write(s2+'\n')
in_f.close
out_f.close
但是无法跳过空行,
也无法顺利写入资料
目前还搞不清楚问题在哪里...
有请高人指点迷津...Orz
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 75.69.103.99
1F:→ kenzou:out_f没有open所以写不进去。 06/23 12:21
2F:推 aitjcize:跳空行: if not line.strip(): continue 06/23 15:43
3F:→ painkiller:感谢! 06/23 20:55