Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Number Format
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,644 Jul-25-2022, 03:19 PM
Last Post: deanhystad
Photo How can I use 2 digits format for all the number? plumberpy 6 2,332 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  multiple number format conversion oli_action 4 2,560 Aug-11-2020, 05:10 AM
Last Post: perfringo
  Number format william888 3 2,827 Aug-23-2019, 05:33 AM
Last Post: ThomasL
  changing format to Int Scott 3 2,456 Jun-19-2019, 06:33 AM
Last Post: ODIS
  changing { and } in str.format() Skaperen 10 4,993 May-16-2019, 05:07 AM
Last Post: Skaperen
  Counting the number of files related to particular format ambush 3 2,921 Nov-05-2018, 08:58 AM
Last Post: buran
  Date Format Changing Program Not Working pyth0nus3r 2 4,718 Jan-28-2017, 10:34 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020