3

I wrote a script to take files of data that is in columns and plot it depending on which column the user wants to view. Well, I noticed that the plots look crazy, and have all the wrong numbers because python is ignoring the exponential.

My numbers are in the format: 1.000000E+1 OR 1.000000E-1

What dtype is that? I am using numpy.genfromtxt to import with a dtype = float. I know there are all sorts of dtypes you can enter, but I cannot find a comprehensive list of the options, and examples.

Thanks.

Here is an example of my input (those spaces are tabs):

Time StampT1_ModBtT2_90BendT3_InPET5_Stg2Rfrg
5:22 AM2.115800E+21.400000E+01.400000E+03.035100E+1
5:23 AM2.094300E+21.400000E+01.400000E+03.034800E+1
5:24 AM2.079300E+21.400000E+01.400000E+03.031300E+1
5:25 AM2.069500E+21.400000E+01.400000E+03.031400E+1
5:26 AM2.052600E+21.400000E+01.400000E+03.030400E+1
5:27 AM2.040700E+21.400000E+01.400000E+03.029100E+1

Update I figured out at least part of the reason why what I am doing does not work. Still do not know how to define dtypes the way I want to.

import numpy as np

file = np.genfromtxt('myfile.txt', usecols = (0,1), dtype = (str, float), delimiter = '\t')

That returns an array of strings for each column. How do I tell it I want column 0 to be a str, and all the rest of the columns to be float?

3 Answers 3

1
In [55]: type(1.000000E+1)
Out[55]: <type 'float'>

What does your input data look like, it's fair possible that it's in the wrong input format but it's also sure that it's fairly easy to convert it to the right format.

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

2 Comments

It looks like columns of numbers. Ok, sure, there is one header line, which I skip. The first column is a Timestamp in this format: 12:00 AM, but I load that as dtype = str, and it works just fine. If the number is 1.000000E+1, the number it will plot is 1.000000, not 10...
Edit your question and copy/paste from your text file.
1

Numbers in the form 1.0000E+1 can be parsed by float(), so I'm not sure what the problem is:

>>> float('1.000E+1')
10.0

1 Comment

Thanks for pointing that out, I am a total newbie and it did not occur to me as a check. But now I am more bewildered...
0

I think you'll want to get a text parser to parse the format into a native python data type.

like 1.00000E+1 turns into 1.0^1, which could be expressed as a float.

Comments

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.