看板 Python 關於我們 聯絡資訊
我是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
kenzou:out_f沒有open所以寫不進去。 06/23 12:21
aitjcize:跳空行: if not line.strip(): continue 06/23 15:43
painkiller:感謝! 06/23 20:55