0

I have a file that looks like this, it has around 80,000 lines:

-1.1361818e-001  4.1730759e-002 -9.8787775e-001  9.7195663e-002
-1.1361818e-001  4.1730759e-002 -9.8787775e-001  9.7195663e-002
-1.1361818e-001  4.1730759e-002 -9.8787775e-001  9.7195663e-002
-1.1361818e-001  4.1730759e-002 -9.8787775e-001  9.7195663e-002

I'd like to work with numpy and scikit, and would like to write the file into an array so that it looks like this:

array = [[-1.1361818e-001,4.1730759e-002,-9.8787775e-001,9.7195663e-002],[-1.1361818e-001 ,4.1730759e-002,-9.8787775e-001,9.7195663e-002]...]

I found the following example at https://stackoverflow.com/a/10938021/1372560

I tried to adapt it to my example:

import numpy as np
a = np.loadtxt("/path2file", delimiter="\t")
print a

And I get the error "ValueError: invalid literal for float(): -1.1361818e-001 4.1730759e-002 -9.8787775e-001 9.7195663e-002"

I'm really stuck here and appreciate your help. Thanks a lot in advance!

  • Obviously, your delimiter is not a tab ("\t"). Just use the default delimiter (that is, don't specify it), which is whitespace, and you should be fine. – user707650 Nov 26 '13 at 13:00
3

That works for me :

import numpy as np
a = np.loadtxt("a.txt")
print a

Output:

[[-0.11361818  0.04173076 -0.98787775  0.09719566]
 [-0.11361818  0.04173076 -0.98787775  0.09719566]
 [-0.11361818  0.04173076 -0.98787775  0.09719566]
 [-0.11361818  0.04173076 -0.98787775  0.09719566]]
  • Thanks a lot! Such an easy solution... – Kaly Nov 26 '13 at 13:18
1

Simply leave the delimiter field empty, then it'll split at any whitespace. \t is a whitespace character only.:

Demo:

>>> import numpy as np
>>> from StringIO import StringIO                                                     
>>> c = StringIO("1.234\t1.23456  1.234234")                                                 
>>> np.loadtxt(c)                                                                           
array([ 1.234   ,  1.23456 ,  1.234234])

From docs:

delimiter : str, optional
    The string used to separate values. By default, this is any whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.