HELP Converting str to float from a

This is the place for queries that don't fit in any of the other categories.

HELP Converting str to float from a

Postby eangola » Thu Aug 15, 2013 5:24 pm

Hi, I wrote this code on MatLab to storage data from certain file, organize it into matrices and then plot WindRose graphs. I am new to Python and I am trying to translate the MatLab code into python. Here's what I got so far:

Code: Select all
f = open ('//home//enrique//Desktop//WindCube//WLS7-94_merged_sta_files//WLS7-94_2012_01_02__00_10_00.sta')
data = [] # Define list
from numpy import * # Open numpy for later data handling
x = True
while x == True: # Read file until you reach 'Altitudes' (get rid of header')
    line = f.readline()
    Altitudes = line
    if str.find(line,'Altitudes') == 0:
        x = False
header = f.readline() # Get rid of last line of header
while line: #data starts, read data until the file is over
    line = f.readline() #read line by line
    delimitador = str.find(line,':') # delimiter to get rid of date when data was collected
    data.append(line[delimitador+4:]) #storage each line of data into a list
f.close()


As you can see, I got what matters from the data into a list of objects (basically lines of strings). what I did in MatLab was creating a loop that converts each line to double (str2num) and storages into a matrix, basically transform each line into a vector keeping the same format. I have no idea how to do this in matlab; the approach I am trying to take is to create a cellarray and storage each line as a row. The problem is, I have to convert the str into float. I tried the following:

Code: Select all
[float(i) for i in data[1]]


Tried picking object 1 from the list, but it doesn't work, it says it cant convert str into float. Some of the values into the data are empty or 'NaN', since it is formatted to be read into matlab. So I guess the problem is the 'NaN', is there any way I could get read of the NaN??? I did somthing similar in matlab once using string functions, but I have no clue of how to do it in python :cry: .

Help please!!!

Thank you.
eangola
 
Posts: 4
Joined: Wed Aug 14, 2013 8:32 pm

Re: HELP Converting str to float from a

Postby micseydel » Thu Aug 15, 2013 6:05 pm

Python will happily accept NaN floats
Code: Select all
>>> float('nan')
nan
>>> float('NaN')
nan

Post your full traceback verbatim, in code tags, and maybe we'll be able to provide more help.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: HELP Converting str to float from a

Postby eangola » Thu Aug 15, 2013 6:22 pm

Code: Select all
f = open ('//home//enrique//Desktop//WindCube//WLS7-94_merged_sta_files//WLS7-94_2012_01_02__00_10_00.sta')
data = [] # Define list
from numpy import * # Open numpy for later data handling
x = True
while x == True: # Read file until you reach 'Altitudes' (get rid of header')
    line = f.readline()
    Altitudes = line
    if str.find(line,'Altitudes') == 0:
        x = False
header = f.readline() # Get rid of last line of header
while line: #data starts, read data until the file is over
    line = f.readline() #read line by line
    delimitador = str.find(line,':') # delimiter to get rid of date when data was collected
    data.append(line[delimitador+4:]) #storage each line of data into a list
f.close()

print data[1]
a = [float(i) for i in data[1]]



Code: Select all
>>>
24.3   NaN   NaN   NaN   0   3.66   0.93   6.08   167.9   1.72   1.72   -21.2   -22.9   2.32   23      3.89   0.94   5.95   169.5   1.72   1.72   -21.1   -23   2.17   16      4.34   0.73   6.22   170.4   1.49   1.49   -20.9   -23   2.25   11      4.46   0.91   6.16   166.3   0.63   0.63   -20.6   -22.8   2.13   8      5.72   1.42   7.9   170.6   1.22   1.22   -20.8   -23   2.22   5      6.66   0.47   7.32   177.5   1.49   1.14   -20.7   -22.9   2.28   2      7.45   0.88   9.98   185.5   1.14   0.6   -20.9   -22.9   1.88   2      NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN      NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN      NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN      NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN      NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN

Traceback (most recent call last):
  File "/home/enrique/Documents/Python/scan file.py", line 18, in <module>
    a = [float(i) for i in data[1]]
ValueError: could not convert string to float: .


Aparently the dots are the problem. From my lack of knowledge this does't make any sense to me, why are dots issues since I am converting to float not to integer???? is it character by character??? I think I know what the issue is, but I have no clue how to solve this!
eangola
 
Posts: 4
Joined: Wed Aug 14, 2013 8:32 pm

Re: HELP Converting str to float from a

Postby micseydel » Thu Aug 15, 2013 6:28 pm

data[1] is a string, so yes, you're passing each character of the string to float(). What you want to do instead is call str.split() on data[1], which will give you a list of the values as strings, which then will make more sense to call float() on.

Try
Code: Select all
a = [float(i) for i in data[1].split()]
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: HELP Converting str to float from a

Postby eangola » Thu Aug 15, 2013 6:41 pm

I tried

a = [float(i) for i in data[1].split]

Code: Select all
Tareceback....
File....line 19, in <module>
a = [float(i) for i in data[1].split]
TypeError: 'builtin_function_or_method' object is not iterable


I also tried

Code: Select all
for i in data[i]:
    a = float(data[i].split)


got

Code: Select all
Tareceback....
File....line 19, in <module>
    for i in data[i]:
TypeError: list indices must be integers, not str


Thank you for answering.
eangola
 
Posts: 4
Joined: Wed Aug 14, 2013 8:32 pm

Re: HELP Converting str to float from a

Postby eangola » Thu Aug 15, 2013 6:49 pm

I forgot the '()' in split. now it works :) Thank you! I am one step forward now.

I can't seem to understand why

Code: Select all
for i in data[i]
    a = float(data[1].split())


Won't work
eangola
 
Posts: 4
Joined: Wed Aug 14, 2013 8:32 pm

Re: HELP Converting str to float from a

Postby stranac » Fri Aug 16, 2013 12:01 pm

Code: Select all
data[1].split()
is a list.
float() returns a floating point number, so it can't be used on a list.

If you want more matlab-ish behavior, you should use numpy arrays instead of lists.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: HELP Converting str to float from a

Postby tnknepp » Fri Aug 16, 2013 3:02 pm

OH MY GOODNESS!! I recognize those file names! He's reading LEOSPHERE data!!! Ha, good luck! :lol:

I think there are better ways of reading in the data, and I have been considering trying my hand at a Python routine to do so. If I get time to work on this I will try to remember to send a copy your way to see how it compares.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 133
Joined: Mon Mar 11, 2013 7:41 pm

Re: HELP Converting str to float from a

Postby stranac » Fri Aug 16, 2013 6:58 pm

Now I'm curious.
But I can't find anything that could be "LEOSPHERE data" :(
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: HELP Converting str to float from a

Postby tnknepp » Mon Aug 19, 2013 12:11 pm

stranac wrote:Now I'm curious.
But I can't find anything that could be "LEOSPHERE data" :(


I know this from his posted path ('//home//enrique//Desktop//WindCube//WLS7-94_merged_sta_files//WLS7-94_2012_01_02__00_10_00.sta') LEOSPHERE is a French company that build wind LIDARs (tells you wind speed/direction in all three axes from the surface to several kilometers). One of their models is the WindCube (WLS7), and the data are stored in ".sta" files, which are just ASCII files that have somewhat horrendous formatting (ok, its not THAT bad, but it could be better to store data in .hdf). It's actually a nice instrument if you have $250,000 laying around (in science this is considered "reasonably priced"), and I've had the opportunity to work with them a couple times. Their processing software is written solely in MatLab though.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 133
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 2 guests