0

I'm reading in data from a .txt file that looks like this:

Time    Date    Inlet Gas   skin #1 SKIN #2 OUT GAS 
        °C  °C  °C  °C  
15:28:55    4/11/2015    2.826471e+001   2.217617e+001   2.408844e+001   2.771613e+001  

When I read it in, I take the time and date and combine them for a strptime object and form a new dict. I also read the labels from the first line and use those as the keys for the new dict. I'm getting an error later in the script that says that to round the values, a float is required. When I flag the "float(a) for a" line, the variable explorer is telling me that the "type" of a is a string_ and its value is " 2.826471e+001" (quotes mine). I tried the ast eval option and it did not work.

dict_labels = [label for label in labels if not label == 'Time' and not label == 'Date' and not label == '']    
current_array =np.array(current_array)    
temp_dict = {}

temp_dict['Dates_Times'] = [datetime.strptime(i + ' ' + j, dateformat) for i,j in zip(current_array[:][:,labels.index('Date')], current_array[:][:,labels.index('Time')])]
for label in dict_labels:
    temp_dict[label] = [float(a) for a in current_array[:][:,labels.index(label)]]
2
  • 1
    >>> float("2.826471e+001") 28.26471 That input can be parsed just find. I think something else is incorrect in your data structures and algorithms. Commented Feb 4, 2016 at 18:54
  • Note that float() returns an object, and a list comprehension creates a new list object. The list comprehension doesn't change the object referenced by the variable current_array. Perhaps after creating floats in the list comprehension you then proceed to use the strings in the original array instead of the new list? Commented Feb 4, 2016 at 18:56

2 Answers 2

1

One option is to split the string, and then work out the math yourself.

i.e. 2.826471e+001 is equal to 2.826471 * 10^1

So use the code:

temp_dict[label] = [float(a.split('e+')[0])*pow(10, int(a.split('e+')[1])) for a in current_array[:][:,labels.index(label)]]

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

1 Comment

I did end up using split because there was a leading space. It turns out that it was hanging up on a different variable later...my datetime objects.
0

There is some other problem, that string is easily cast in both Python 2 and 3.

>>> float("2.826471e+001")
28.26471

2 Comments

Does this deserve to be an answer?
It answers the question. Perhaps the question is poor.

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.