作者Dong0129 (阿东)
看板Python
标题[问题] File1内容跳一行再加File2内容
时间Fri Apr 17 10:45:09 2015
请问各位版友,
我有两个档案,
File1: File2:
1 5
2 6
3 7
4 8
要合并成:
File3:
1 5
2 6
3 7
4 8
目前的code:
rfd1=open("file1","r")
rfd2=open("file2","r")
wfd=open("file3","w")
for i in rfd1:
if i[-1]=='\n':
i=[0:-1]
wfd.write(i)
for i in rfd2:
wfd.write('\t'+i)
break
rfd1.close()
rfd2.close()
wfd.close()
目前想出来也可用的程式码如上,
但在思考是否有更好更短的写法呢??
还算是python初学者...所以写的不够好请见谅!!
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.137.113.177
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1429238713.A.B92.html
※ 编辑: Dong0129 (220.137.113.177), 04/17/2015 10:45:58
1F:→ alibuda174: for x in zip(rfd1, rfd2): 04/17 11:06
2F:→ alibuda174: f3.write(x[0].rstrip('\n') + '\t' + x[1]) 04/17 11:06
3F:→ alibuda174: 抱歉 f3改成wfd 04/17 11:08
因为会的语句还不够多所以只用比较粗糙的写法,
ZIP下面有版友有分享,
之前有看过,但是不知道这个词是做何用所以就不使用...
※ 编辑: Dong0129 (220.137.113.177), 04/17/2015 12:27:13