Python Forum

Full Version: Changing Number Format
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm strictly a hardware type and am caught in a conundrum trying to capture data from my oscilloscope.
I know nothing about Python, but found a workable solution on EEVblog.
Sample code which samples Channel 1 and writes to a CSV file :
   # Read DS1054Z Channel 1
    telnetToInstrument.write(":MEAS:ITEM? VAVG, CHAN1\n")
    buff = telnetToInstrument.read_until("\n", maxWaitForAnswer)
    csvLine += "," + buff[:-1]
Now this works just fine but has one problem :
It writes in scientific notation (nnnEXPmmm)
Which is really hard to read (logging the output of a photovoltaic panel)
How can I make it write in standard decimal ie NN.NN (13.65 volts for instance)
Any help appreciated.
Thanks, Mike
Maybe. From the way buff is handled on line 4, it looks like a string. You could confirm this with print(type(buff)) after line 3, print(repr(buff)) might help too. You might be able to convert that string to a floating point number, and then use the format method of strings, or the even newer f-string syntax (Python 3.6+), to control the formatting output. That might lose some precision though. You would have to modify the string before converting to a float, because that's not a format the float function recognizes. float(buff.replace('EXP', 'E')) might work for that.
Sample of output (Timestamp + 4 data channels)
11:33:55,1.000000e-01,1.488763e+01,2.986618e-01,3.000000e-01
11:33:56,1.000000e-01,1.488763e+01,2.986618e-01,3.000000e-01
11:33:57,1.000000e-01,1.488612e+01,2.994980e-01,3.000000e-01
11:33:58,1.000000e-01,9.411707e+00,2.989960e-01,3.000000e-01
11:33:59,9.306022e+00,1.000000e-01,2.991638e-01,3.000000e-01
11:34:00,1.495050e+01,1.000000e-01,2.984955e-01,3.000000e-01
11:34:01,1.494833e+01,1.000000e-01,2.986618e-01,3.000000e-01
That appears to be the csvline output. I can't be sure what's going on without those two print statements I mentioned (or a link to the documentation for whatever package you are using).
Example for 4 data channels.
>>> n = '1.000000e-01,1.488763e+01,2.986618e-01,3.000000e-01'
>>> n = n.split(',')
>>> n
['1.000000e-01', '1.488763e+01', '2.986618e-01', '3.000000e-01']
>>> n = [float(i) for i in n]
>>> n
[0.1, 14.88763, 0.2986618, 0.3]
>>> for item in n:
...     print(f'{item:.2f}')
...     
0.10
14.89
0.30
0.30
Date covert to be a little fancy Pendulum.
>>> import pendulum
>>> 
>>> dt = pendulum.parse('11:33:55', exact=True)
>>> dt
Time(11, 33, 55)
>>> dt.minute
33