将文本文件转换为numpy数组 [英] Convert a text file to a numpy array

查看:612
本文介绍了将文本文件转换为numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串列表转换为数组.该列表实际上是一个数字数组,它是我从文本文件中获取的n行长4列的数字.我需要将此列表转换为n行乘4列且为浮点型的数组.下面是到目前为止的代码:

I am trying to convert a list of strings into an array. The list is really an array of numbers that is n rows long by 4 columns that I took from a text file. I need to convert this list to an array that is n rows by 4 columns and is floating type. Below is my code so far:

#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)   

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

This is where I am having problems. The resultant array is a 1D array with all the information from each line in the list jumbled together.

这是输入文件格式的示例.

Here is an example of the input file format.

## =============================================================================
## 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)作为浮点.示例:

I want an array (11 x 4) of this data as a floating point. Example:

   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

推荐答案

可能您只需要numpy.loadtxt函数:它对您发布的输入内容有效:

It worked for me on the input you posted:

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]])

这篇关于将文本文件转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆