6

I have a text file which contains 2 columns separated by a tab, containing some data that I would like to read into arrays and perform some simple operations for instance plot the data. The data in the second column is in scientific notation and can takes extremely small values such varying from order of magnitude 10e-27 10e-50. For instance here is a sample of the data

0.00521135   -1.197189e-31
0.00529274   -7.0272737e-32
0.00530917   -6.0163467e-32
0.00532565   -4.9990405e-32
0.00534218   -3.9747722e-32
0.00535876   -2.9457271e-32
0.0053754    -1.9094542e-32
0.00539208   -8.6847519e-33
0.00540882    1.7851373e-33
0.00542561    1.2288483e-32
0.00544245    2.2850705e-32
0.00545934    3.3432858e-32
0.00547629    4.4084594e-32
0.00549329    5.4765499e-32
0.00551034    6.5491709e-32

Here is what my code looks like :

import numpy as np
import matplotlib.pyplot as plt
with open('data.dat', 'r') as f2:
lines = f2.readlines()
data = [line.split()for line in lines] 
data2 = np.asfarray(data)
x1 = data2[:,0]
y1 = data2[:,1]
plt.plot(x1, y1)
plt.show()

I have used this code to test on sample data (in .dat format) files and it seems to work fine, however when I run this code on my data set it gives me the following error.

Traceback (most recent call last):
File "read_txt_col.py", line 17, in <module>
data2 = np.asfarray(data)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages  /numpy/lib/type_check.py", line 103, in asfarray
return asarray(a,dtype=dtype)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numeric.py", line 235, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

Could someone please help!!

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
user2818607
  • 61
  • 1
  • 2
  • There are plenty of SO answers on this: http://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence http://stackoverflow.com/questions/13310347/numpy-valueerror-setting-an-array-element-with-a-sequence-this-message-may-app THe basic problem is that you appear to have sequences of different lengths. I would try to find the problem in your data by binary search: load half the data. Successful? Add half of the data and test that. Successful? Add another half, etc. – hughdbrown Sep 26 '13 at 08:59

1 Answers1

8

Don't reinvent the wheel!, it would be much more easy to use numpy.loadtxt:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> data = np.loadtxt('data.dat')
>>> x1 = data[:,0]
>>> y1 = data[:,1]
>>> plt.plot(x1, y1)
>>> plt.show()

enter image description here

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
  • For some more power, especially in error checking it is worth mentioning [numpy.genfromtxt()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html) – Greg Sep 26 '13 at 11:55