2013-10-29 39 views
1

如何在python/numpy中創建一個文本文件,並排顯示多個1D數組,並排對齊列(即用空格分隔)。我還想在列的頂部包含數組的名稱。Python/Numpy - 將多個1D數組寫入整齊列中的文件

這是一個我一直在使用的例子。 (請注意,在一個字符串[「現場」]數組是不同的字符的長度,從而導致不對齊列)

import numpy as np 
dt = np.dtype([('site', '|S11'), ('year', 'i'), ('dat1', 'd'), ('dat2', 'd')]) 
a = np.zeros(2, dt) 
a['site'] = ['Paris', 'London'] 
a['year'] = [1979, 1980] 
a['dat1'] = [272.4322, 270.36] 
a['dat2'] = [2.21, 3.55] 
np.savetxt('test.txt', a, '%s') 

理想我想的東西,可以產生這樣的文件:http://www.antarctica.ac.uk/data/absl/ABSL-index-Monthly-ERA-Interim_Hosking2013.txt

我現在已經找到了一個答案的問題,請參見下面的... https://stackoverflow.com/a/19676112/1310153

+1

你應該看看:[熊貓](http://pandas.pydata.org/) [to_csv](http://pandas.pydata.org/pandas-docs/stable/generated /pandas.DataFrame.to_csv.html?highlight=to_csv#pandas.DataFrame.to_csv) 您可以將數組放入'Series' =>到'DataFrame'中,然後放到一個csv文件中。 或嘗試設置'np.savetxt('test.txt',a,fmt ='%s',delimiter ='\ t') –

+0

https://code.google.com/p/prettytable/可能會訣竅與調整 – wim

+1

你可以給一個輸出的例子? – Christian

回答

2

試試這個:

import numpy as np 

dt = np.dtype([('site', '|S11'), ('year', 'i'), ('dat1', 'd'), ('dat2', 'd')]) 
a = np.zeros(2, dt) 
a['site'] = ['Paris', 'London'] 
a['year'] = [1979, 1980] 
a['dat1'] = [272.4322, 270.36] 
a['dat2'] = [2.21, 3.55] 

np.savetxt('test.txt', a, '%10s') 

'%10s'10是字段寬度。

2

你可以這樣做:

header = '#\n# Amundsen-Bellingshausen Seas Low (ABSL) Monthly Index\n# (based on ERA-Interim Reanalysis data)\n#\n# Dr J. Scott Hosking\n# British Antarctic Survey\n#\n# For more inforation see:\n# Hosking et al., 2013: The influence of the Amundsen-Bellingshausen Seas Low on the\n# climate of WestAntarctica and its representation in coupled climate model simulations, J.Climate\n#\n# Updated dataset can be found at: http://www.antarctica.ac.uk/data/absl/\n#\n# Key:\n# ABSLSectorP = Area-average MSLP over ABSL sector (see Fig. 2e)\n# ActCenPres = Actual central pressure (i.e., ABSL Minumum MSLP)\n# RelCenPres = Relative central pressure (ActCenPres "minus" ABSLSectorP)\n# ABSL_long = Longitudinal location of ABSL (degrees East)\n# ABSL_Lat = Latitudinal location of ABSL\n#\n\nModel  Year Month ABSLSectorP ActCenPres RelCenPres ABSL_long  ABSL_Lat\n' 
np.savetxt('test.txt', a, header=header, fmt=('%s, %d, %f, %f')) 

或者:

np.savetxt('test.txt', a, fmt='%-12s') 

然後你有上述數據的列名。

+1

我會使用'fmt ='% - 12s%d%f%f'',這樣數字的間距是自動的,但固定的字符串的寬度。 – askewchan

1

如果你想,說,各元素之間的兩個選項卡,你可以這樣做:

>>> with open('testfile.txt','w') as f: 
    f.write('your header string') 
    for x in a: 
     for y in x: 
      f.write(str(y)+'\t\t') 
     f.write('\n') 

或者,對@Jan Zeiseweis'評論擴展,你可以使用熊貓:

import pandas as pd 
pd.DataFrame(a).to_csv('testfile.txt',sep='\t') 
0

現在我已經找到了使用與列數據相同的格式來包含標題的簡潔方法。謝謝大家的幫助。

import numpy as np 
dt = np.dtype([('site', '|S11'), ('year', 'i'), ('dat1', 'd'), ('dat2', 'd')]) 
a = np.zeros(2, dt) 
a['site'] = ['Paris', 'London'] 
a['year'] = [1979, 1980] 
a['dat1'] = [272.4322, 270.36] 
a['dat2'] = [2.21, 3.55] 

titles = ['Site', 'Year', 'Dat1', 'Dat2'] 
header = '%-12s %6s %11s %11s' % (tuple(titles)) 
header = ''+header+'\n' 
np.savetxt('test.txt', a, comments='', header=header, fmt='%-12s %6i %11.4f %11.4f')