2

I need to read a file that has a structure like:

 1 2 3 4 5
 6 7 8 9 10
 11 22
 13 14 15 16 17
 18 19 20 21 22
 23 24

I need to read this file in a single array = [ 1,2,3, ... , 23, 24]

How to do that in numpy ?? Usin:

Array = np.genfromtxt(pathToFile, dtype=float, skip_header=1, comments='/')

Didn't work:

Line #796537 (got 2 columns instead of 5)
0

4 Answers 4

5

Easier way:

result=np.fromfile(path_to_file,dtype=float,sep="\t",count=-1)
Sign up to request clarification or add additional context in comments.

Comments

2

Use np.fromstring instead?

>>> np.fromstring(''.join(open('yourfile.txt', 'r').read().splitlines()),sep=" ")


array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
        22.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,
        23.,  24.])

Comments

0

why do you need numpy here?

In [101]: with open('data1.txt') as f:
    lis=[float(y) for x in f for y in x.split()]
    print lis
   .....:     
   .....:     
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 22.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0]

4 Comments

The data here is just a example, the number are floats, and the file has 4 milion numbers, removed the ">" as they are not in the data..
if the file has floats then use float() instead of int(), I don't think using numpy here will have any advantage over this method.
@canesin what exactly you wanna do with those 4 million numbers, because storing those many numbers is not memory efficient.
I want to read a file that contains information about locations, but I only know the position of it in a 1D array... I believe that acessing a specific position in the file gonna be harder
0

Remove new lines and save to another file:

open('ofile.txt','w').write(''.join(open('infile.txt', 'r').read().splitlines()))

Then it will work:

>>> np.genfromtxt('ofile.txt')
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
    22.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,
    23.,  24.])

Comments

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.