2010-08-13 64 views
1

我有製表符分隔的數據,我將選定的幾列導出到另一個文件中。我有:追加數據列

a b c d 
1 2 3 4 
5 6 7 8 
9 10 11 12 

,我也得到:

b, d 
b, d 
2, 4 
b, d 
2, 4 
6, 8 
b, d 
2, 4 
6, 8 
10, 12 
...... 

我想:

b, d 
2, 4 
6, 8 
10, 12 

我的代碼是

f=open('data.txt', 'r') 
f1=open('newdata.txt','w') 
t=[] 
for line in f.readlines(): 
    line =line.split('\t') 
    t.append('%s,%s\n' %(line[0], line[3])) 
    f1.writelines(t) 

我在做什麼錯???它爲什麼重複?

PLease help

Thanks!

回答

4

縮進是錯誤的,所以你正在寫每個迭代的整個數組t,而不是隻在最後。它改成這樣:

t=[] 
for line in f.readlines(): 
    line = line.split('\t') 
    t.append('%s,%s\n' % (line[0], line[3])) 
f1.writelines(t) 

或者你可以寫一個線在同一時間,而不是等到最後,則不需要陣列t可言。

for line in f.readlines(): 
    line = line.split('\t') 
    s = '%s,%s\n' % (line[0], line[3]) 
    f1.write(s) 
+0

太謝謝你了!我知道它必須是一件非常簡單的事情。 – 2010-08-13 11:06:23

1

如上所述,最後一行是不正確的縮進。最重要的是,你正在使事情變得困難和容易出錯。您不需要t列表,而且您不需要使用f.readlines()

與您的代碼的另一個問題是,你的line[3]將與新行結束(因爲readlines方法()和朋友離開換行符在該行的結束),並且您在格式'%s,%s\n'增加另一個新行......這會在你的輸出文件中產生兩倍的間距,但是你沒有提到。

另外你說你要在第一輸出線b, d,和你說,你得到b, d - 但是你的代碼說'%s,%s\n' %(line[0], line[3])將產生a,d。注意兩個區別:(1)缺少空格(2)a而不是b。總的來說:你說你得到b, d\n,但是你顯示的代碼會產生a,d\n\n。將來,請顯示相互對應的代碼和輸出。使用複製/粘貼;不要從內存中鍵入。

試試這個:

f = open('data.txt', 'r') 
f1 = open('newdata.txt','w') 
for line in f: # reading one line at a time 
    fields = line.rstrip('\n').split('\t') 
    # ... using rstrip to remove the newline. 
    # Re-using the name `line` as you did makes your script less clear. 
    f1.write('%s,%s\n' % (fields[0], fields[3])) 
    # Change the above line as needed to make it agree with your desired output. 
f.close() 
f1.close() 
# Always close files when you have finished with them, 
# especially files that you have written to.