3

I'm really new with python. So maybe my question is really basic...For my work I'm checking different parameters in a period of time. For the Beginning with Python I wanted to plot a simple List with daily measured Temperature-values for a month. In the List I've three splits like these following structure:

Day -TAB- Temperature -TAB- Nr

My Code:

import pylab as pl
import numpy as np

filename = "u_netCDF_write"
file = open(filename)
NoOfValues = 31                                                      

counter=0
data = []              
for line in file:                                                     
    if counter <= NoOfValues:                                          
         data.append(line.strip('\n').strip('\t').split(' '))            
         if len(data[-1]) == 4:                                         
            data[-1].pop(3)                                             
    counter+=1                                                          
x = np.linspace(0,30,31)                                                
data = np.transpose(data)                                             

for i in range(len(data[2])):                                           
   data[2][i] = float(data[2][i])-273.15

When I try to plot a Temperature-per-Day-Plot I get the Error-Message:

Traceback (most recent call last):
  File ".../.../unetCDFplot.py", line 43, in <module>
    data[2][i] = float(data[2][i])-273.15   
ValueError: invalid literal for float(): 03.07.2014

It looks like that the Code didn't transpose the Data. Why is that so? Can anybody help me? Thanks!

Liam
  • 6,009
  • 4
  • 39
  • 53
elly
  • 166
  • 1
  • 2
  • 15

3 Answers3

3

I solved my problem! So for anybody who has the same problem, here is what I did: I used

print(repr(data))

(thanks to Stephany Dionysio) to check every step in my code and understood that the problem was not the transpose-function, but the empty spaces in every line. After trying different methods to delete the empty spaces I saw that I couldn't delete an array in an array caused by 'data.append'. To get the values I needed I used pop() in the append method:

data.append(line.strip('\n').strip('\t').split(' ').pop(7))  

Now my code works fine. Thank you for your good advices, they put me to the right way! :)

elly
  • 166
  • 1
  • 2
  • 15
2

I don't know the content of your "u_netCDF_write" file so it is reasonably dificult to debug it. But as the other post shows, it cloud be a non-printing character that's present in the value.

See if this helps python ValueError: invalid literal for float()

Community
  • 1
  • 1
SZD
  • 81
  • 4
0

03.07.2014 cannot be a float. It looks like you're using the wrong data from your data list.

LegendaryDude
  • 562
  • 8
  • 23