将文本文件转换为numpy数组

时间:2014-04-10 17:33:16

标签: python arrays numpy

我正在尝试将字符串列表转换为数组。该列表实际上是一个数字数组,我从文本文件中取出了4行长4列。我需要将此列表转换为n行乘4列的数组,并且是浮动类型。以下是我目前的代码:

#Calculate the average velocity through a tidal cycle

from pylab import *
import numpy as np

#Open profile1.ele and get the data
lookup = '##'
data = []
eleline = []

with open('profile1.ele') as f:
for line in f:
    if not line.startswith(lookup):   #disclude lines with '##'
        data.append(line.rstrip("\r\n"))  
    if 'Elements' in line:
        eleline.append(line)
s = ''.join(eleline)   #Convert list to string
numele = s.rsplit()[-2] #Grab # of elements
numele = int(numele) #convert to integer

#Convert data list object into an array
elements = np.asarray(data)   

这是我遇到问题的地方。结果数组是一维数组,列表中每行的所有信息混杂在一起。

以下是输入文件格式的示例。

## =============================================================================
## TIME STEP      1         Duration:  6.0000E+02 sec      Time:  3.4712E+09 sec
## =============================================================================
##      X origin       Y origin     X velocity     Y velocity
   3.1225530E-01 -9.5153722E+00  4.8239441E-09 -1.1614215E-08
   4.0205122E-01 -8.5404981E+00  1.7396887E-09 -1.8665899E-08
   4.3224251E-01 -7.5565436E+00  2.0985602E-09 -2.5349955E-08
   4.3234870E-01 -6.5693932E+00  1.7166213E-09 -3.1156361E-08
   4.2276193E-01 -5.5905580E+00  1.9627062E-09 -3.7317066E-08
   4.0245047E-01 -4.6585868E+00  1.7305504E-09 -4.3153198E-08
   3.6284562E-01 -3.8494609E+00  1.7422198E-09 -4.8249619E-08
   3.1234937E-01 -3.1767707E+00  1.9901861E-09 -5.3221055E-08
   2.6726067E-01 -2.5743939E+00  1.9799420E-09 -5.8343627E-08
   2.2791616E-01 -2.0380240E+00  1.7150138E-09 -6.3250542E-08
   1.8285348E-01 -1.5997592E+00  9.9428594E-10 -6.7249257E-08

我希望将此数据的数组(11 x 4)作为浮点数。例如:

   3.1225530E-01 -9.5153722E+00  4.8239441E-09 -1.1614215E-08
   4.0205122E-01 -8.5404981E+00  1.7396887E-09 -1.8665899E-08
   4.3224251E-01 -7.5565436E+00  2.0985602E-09 -2.5349955E-08
   4.3234870E-01 -6.5693932E+00  1.7166213E-09 -3.1156361E-08
   4.2276193E-01 -5.5905580E+00  1.9627062E-09 -3.7317066E-08
   4.0245047E-01 -4.6585868E+00  1.7305504E-09 -4.3153198E-08
   3.6284562E-01 -3.8494609E+00  1.7422198E-09 -4.8249619E-08
   3.1234937E-01 -3.1767707E+00  1.9901861E-09 -5.3221055E-08
   2.6726067E-01 -2.5743939E+00  1.9799420E-09 -5.8343627E-08
   2.2791616E-01 -2.0380240E+00  1.7150138E-09 -6.3250542E-08
   1.8285348E-01 -1.5997592E+00  9.9428594E-10 -6.7249257E-08

1 个答案:

答案 0 :(得分:4)

您可能只需要numpy.loadtxt功能:http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html#numpy.loadtxt

根据您发布的输入,它对我有用:

In [1]: import numpy as np

In [2]: a = np.loadtxt('example.csv')

In [3]: a
Out[3]:
array([[  3.12255300e-01,  -9.51537220e+00,   4.82394410e-09,
         -1.16142150e-08],
       [  4.02051220e-01,  -8.54049810e+00,   1.73968870e-09,
         -1.86658990e-08],
       [  4.32242510e-01,  -7.55654360e+00,   2.09856020e-09,
         -2.53499550e-08],
       [  4.32348700e-01,  -6.56939320e+00,   1.71662130e-09,
         -3.11563610e-08],
       [  4.22761930e-01,  -5.59055800e+00,   1.96270620e-09,
         -3.73170660e-08],
       [  4.02450470e-01,  -4.65858680e+00,   1.73055040e-09,
         -4.31531980e-08],
       [  3.62845620e-01,  -3.84946090e+00,   1.74221980e-09,
         -4.82496190e-08],
       [  3.12349370e-01,  -3.17677070e+00,   1.99018610e-09,
         -5.32210550e-08],
       [  2.67260670e-01,  -2.57439390e+00,   1.97994200e-09,
         -5.83436270e-08],
       [  2.27916160e-01,  -2.03802400e+00,   1.71501380e-09,
         -6.32505420e-08],
       [  1.82853480e-01,  -1.59975920e+00,   9.94285940e-10,
         -6.72492570e-08]])