Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error with reading float from two column text file into an array in Python

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

like image 984
user2818607 Avatar asked Sep 26 '13 08:09

user2818607


People also ask

What does the loadtxt() function do in NumPy?

loadtxt() function. The loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.

How do you float an array in Python?

You need x = np. zeros(N) , etc.: this declares the arrays as float arrays. This is the standard way of putting zeros in an array ( np. tile() is convenient for creating a tiling with a fixed array).

What does NumPy loadtxt return?

The function returns an n-dimensional NumPy array of values found in the text.


1 Answers

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

like image 194
jabaldonedo Avatar answered Oct 12 '22 00:10

jabaldonedo