开发者

loading tab separated text files with multipled data into arrays in python

I want to load tab separated input file as an array into python and I know that genfromtxt in numpy does that but the problem is that I have multiple sets of data that i would like to be loaded. Basically my sample file could be :

#FILE START

#intensities

11 1 1

0 1 2

#indexes

1 2 3 4 5 6

7 8 9 1 1 2

#FILE END

So i would like to use this file to load the intensities as an array and indexes as another array. I would not like to be aware of th开发者_如何学Ce number of rows of intensities before hand but i can put the commentary ("intensities" or [intensities] like in ConfigParser to mark where a section starts or end).

Does there exist something like this or I will have to write something of my own?

Thanks


f = open(filepath, 'r')
tags = ["#intensities"]
answer = {}
for line in f:
    if line.strip() in tags: # we've encountered a new tag
        curr = line.strip()[1:]
        answer[curr] = []
    else:
        answer[curr].append(line.strip().split('\t'))

f.close()

Now, answer will look like this:

{'intensities':[['11', '1', '1'], ['0', '1', '2']]}

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜