作者Starwindd (我是G爹)
看板Python
標題Re: [問題] 如何讀取txt檔中的資料並寫入另一個txt檔?
時間Wed Aug 4 22:39:52 2010
with open('1.txt','r') as in_f:
with open('2.txt','w') as out_f:
for line in in_f:
print line;
try:
mylist = line.split(" ")
if len(mylist) >= 3:
out_f.writelines(mylist[0] + " " + mylist[2])
else:
print "not 3 items"
except Exception, e:
print "error: " + str(e)
--
聽好了,你在想這些無聊事的時候,時間正一刻一刻地從我們身旁流過...
這些流過的時間不只是你的生命,也是我的生命啊!
── 炎尾燃
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 131.204.39.159
推 StubbornLin:2.6的with不能用,分開 很討厭 但2.7的就可以 08/04 23:35
→ StubbornLin:看起來就不會縮那麼大 08/04 23:35
→ Starwindd:其實用open也行,只是要記得close 08/05 00:13
推 qrtt1:縮排好長啊 XD 08/05 15:48
→ Starwindd:怪notepad吧 XD 08/06 04:12
應觀眾要求重排一次 ^^;
※ 編輯: Starwindd 來自: 131.204.39.159 (08/06 04:16)