1

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 the 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

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. You made me think that why I just did not code it myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.