读取数据从文本文件numpy的数组 [英] Reading data into numpy array from text file

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

问题描述

我有一些元数据的文件,然后由一些实际的数据2栏与标题。我是否需要使用numpy的前genfromtxt对两种类型的数据分开?或者,可不知何故,我也许分割数据?怎么样把文件指针到行刚刚结束的头部上方,然后从那里试图genfromtxt?谢谢
该文件的格式如下所示:

 &安培; SRS
< MetaDataAtStart>
多=真
波长(埃)= 0.97587
模式=评估
背景= TRUE
issid = py11n2g
嘈杂= TRUE
< / MetaDataAtStart>
&安培; END
两个西塔(度)计算(秒^ -1)
10.0 41.0
10.1 39.0
10.2 38.0
10.3 38.0
10.4 41.0
10.5 42.0
10.6 38.0
10.7 44.0
10.8 42.0
10.9 39.0
11.0 37.0
11.1 37.0
11.2 45.0
11.3 36.0
11.4 37.0
11.5 37.0
11.6 40.0
11.7 44.0
11.8 45.0
11.9 46.0
12.0 44.0
12.1 40.0
12.2 41.0
12.3 39.0
12.4 41.0


解决方案

如果你不希望第一个 N 行,尝试(如果没有丢失数据)

 数据= numpy.loadtxt(yourFileName,skiprows = N)

或者(如果有缺失的数据):

 数据= numpy.genfromtxt(yourFileName,skiprows = N)

如果要在随后解析头信息,你可以回去和打开文件解析的标题,例如:

  FH =开(yourFileName,'R')
对于我,行枚举(FH):
    如果我是N:突破
    do_other_stuff_to_header(线)
fh.close()

I have a file with some metadata, and then some actual data consisting of 2 columns with headings. Do I need to separate the two types of data before using genfromtxt in numpy? Or can I somehow split the data maybe? What about placing the file pointer to the end of the line just above the headers, and then trying genfromtxt from there? Thanks The format of the file is shown below:

 &SRS
<MetaDataAtStart>
multiple=True
Wavelength (Angstrom)=0.97587
mode=assessment
background=True
issid=py11n2g
noisy=True
</MetaDataAtStart>
&END
Two Theta(deg)  Counts(sec^-1)
10.0    41.0
10.1    39.0
10.2    38.0
10.3    38.0
10.4    41.0
10.5    42.0
10.6    38.0
10.7    44.0
10.8    42.0
10.9    39.0
11.0    37.0
11.1    37.0
11.2    45.0
11.3    36.0
11.4    37.0
11.5    37.0
11.6    40.0
11.7    44.0
11.8    45.0
11.9    46.0
12.0    44.0
12.1    40.0
12.2    41.0
12.3    39.0
12.4    41.0

解决方案

If you don't want the first n rows, try (if there is no missing data):

data = numpy.loadtxt(yourFileName,skiprows=n)

or (if there are missing data):

data = numpy.genfromtxt(yourFileName,skiprows=n)    

If you then want to parse the header information, you can go back and open the file parse the header, for example:

fh = open(yourFileName,'r')
for i,line in enumerate(fh):
    if i is n: break
    do_other_stuff_to_header(line)
fh.close()

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

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