Splitting output print

Hey everyone!

I’m now working on logging data from a pressure/vacuum reader. This particular reader (MKS PDR2000) has two channels/transducers and only one output. Currently, script is simple and looks like this:

import serial
import time

ser.close()
ser = serial.Serial(port='COM9',\
                    baudrate=9600,
                    parity=serial.PARITY_NONE,\
                    stopbits=serial.STOPBITS_ONE,\
                    bytesize=serial.EIGHTBITS,\
                    timeout=2)
while True:
    ser.write((('p')).encode('utf-8'))
    out1 = ser.read_until().decode('utf-8')
    print(out1, end="")
    time.sleep(1)
ser.close()

Output is:

 74.1e-3  72.4e-3
 74.1e-3  72.4e-3
 74.1e-3  72.4e-3
 74.1e-3  72.4e-3
 74.1e-3  72.4e-3
 74.1e-3  72.4e-3
 74.1e-3  72.4e-3

Does anyone know a quick way to assign each of the figures on each output line (eg ‘74.1e-3’ and ‘72.4e-3’) to separate cells on a spreadsheet? Thanks in advance!

I’d be tempted to create an object to hold the data as CSVs, then write said object to file. The file could then be opened by your spreadsheet; Voilà

1 Like

Good idea! I’ll have a crack at it

No, worries. If you get stuck, I’ve an idea of how it may work using a list object; well two list objects in fact, as you need the readings (the out1) in pairs, like this:

74.1e-3,72.4e-3
74.1e-3,72.4e-3
74.1e-3,72.4e-3
74.1e-3,72.4e-3
74.1e-3,72.4e-3
74.1e-3,72.4e-3

… right?

Yup, that’s what I had in mind. It would help organize it with 10+ other readings I’m logging. Any hints?

All in pairs, as above? Or is it out2, out3

For the other readings, the monitor they come from is capable of sending individual readings. So those are out1 through out10

Okay, I’m unsure at this point, but I’d suggest cracking out1 for starters, then you may have a light-bulb moment and see how the others can be incorporated.

This is what I have in mind. It’s maybe not the best way, but it should work.

As a note: I use the csv module for reading files; I all but never write csv files from Python, but when I do, I use my own parser, as I’ve not got my head around the module write functions, so…

data = ['74.1e-3', '72.4e-3', '74.1e-3', '72.4e-3', '74.1e-3', '72.4e-3', '74.1e-3',
        '72.4e-3', '74.1e-3', '72.4e-3', '74.1e-3', '72.4e-3', '74.1e-3', '72.4e-3']


with open('output.csv', mode='w') as output:
    for index in range(0, len(data), 2):  # start, stop, step
        # a temp list from which to assemble the CSV entry
        out_list = data[index:index + 2]
        # put the enrty together and write to the file
        out_str = out_list[0] + ',' + out_list[1] + '\n'
        output.write(out_str)

The data list object would in fact be data = [], but I’ve populated it, as it would be if you were to use data.append(out1) where you have print(out1, end="") in your script. I’m also assuming that out1 is a string object, but if it’s not, it can be type cast on the fly, as I’m sure you know.

lkm if that works for you.