python data types
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 (th开发者_开发问答ose spaces are tabs):
Time Stamp
5:22 AMT1_ModBt
T2_90Bend
T3_InPE
T5_Stg2Rfrg
2.115800E+2
1.400000E+0
1.400000E+0
3.035100E+1 5:23 AM
2.094300E+2
1.400000E+0
1.400000E+0
3.034800E+1 5:24 AM
2.079300E+2
1.400000E+0
1.400000E+0
3.031300E+1 5:25 AM
2.069500E+2
1.400000E+0
1.400000E+0
3.031400E+1 5:26 AM
2.052600E+2
1.400000E+0
1.400000E+0
3.030400E+1 5:27 AM
2.040700E+2
1.400000E+0
1.400000E+0
3.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?
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.
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
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.
精彩评论