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

1
  • There are plenty of SO answers on this: stackoverflow.com/questions/4674473/… stackoverflow.com/questions/13310347/… 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. Commented Sep 26, 2013 at 8:59

1 Answer 1

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

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

1 Comment

For some more power, especially in error checking it is worth mentioning numpy.genfromtxt()

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.