2016-09-14 55 views
2

在:How to write numpy arrays to .txt file, starting at a certain line?如何將numpy數組寫入.txt文件,從某一行開始? numpy版本1.6

人們幫助我解決了我的問題 - 這適用於numpy版本1.7或更高版本。可惜的是我必須使用1.6版本 - follwoing代碼(謝謝@Praveen)

extra_text = 'Answer to life, the universe and everything = 42' 
header = '# Filexy\n# time operation1 operation2\n' + extra_text 
np.savetxt('example.txt', np.c_[time, operation1, operation2],  
       header=header, fmt='%d', delimiter='\t', comments='' 

給我一個錯誤與numpy的1.6

numpy.savetxt() got an unexpected keyword argument 'header' · Issue ... 

有一個變通的產生1.6版結果相同:

# Filexy 
# time operation1 operation2 
Answer to life, the universe and everything = 42 
0 12 100 
60 23 123 
120 68 203 
180 26 301 
+0

爲什麼要使用不推薦的numpy?如果是關於管理員權限,請查看pyenv。 –

回答

2

您先寫信頭,然後轉儲數據。 請注意,您需要在標題的每一行中添加#,因爲np.savetxt不會這樣做。

time = np.array([0,60,120,180]) 
operation1 = np.array([12,23,68,26]) 
operation2 = np.array([100,123,203,301]) 
header='#Filexy\n#time operation1 operation2' 
with open('example.txt', 'w') as f: 
    f.write(header) 
    np.savetxt(f, np.c_[time, operation1, operation2], 
        fmt='%d', 
        delimiter='\t') 
相關問題