简体   繁体   中英

Need to write part of a python script that save data and writes it to a file

I am currently using python and qt designer as a way of acquiring concentration values from a colorimeter. My current GUI has three buttons, "Connect", "Calibrate" and "Measure". Whenever I get to the measure part, it displays only one concentration value. If you were to place another sample in the colorimeter, it would delete the previous value and replace it with a new one.

When I write to a file, I am only able to save the last sample I measured, but I want to be able to log every one of the measurements.

This is currently what I have written for this particular part:

def measureSample(self):
    sys.stdout.flush()
    freqD1, trandD1, absoD1 = dev.getMeasurement(LED_TO_COLOR='D1'])
    freqD2, trandD2, absoD2 = dev.getMeasurement(LED_TO_COLOR='D2'])
    absoDiff= absoD1 - absoD2
    Coeff= 1 
    Conc = absoDiff/Coeff
    Conc3SD = '{Value:1.{digits}f'.format(Value = Conc, digits=3)
    self.textEdit.clear()
    self.textEdit.setText('Concentration is {0}.format(Conc3SD))

    timeStr = time.strftime('%m-%d-%Y %H:%M:%S %Z')
    outFile = open('ConcentrationData.txt','w')
    outFile.write('{0} || Concentration: {1}'.format(timeStr, Conc3SD))
    outFile.close

So, my question is, how can I keep a log of every concentration value I take without it appearing on my GUI, AND how can I write them to a file? It's very important that I only display one value, not a list of the previous values on the GUI.

Thanks in advance!

-Marina

The trick lies in the 'w' in your open() call. This means you want to write a new file. Change it to 'a' for appending to an existing file. See here for more info: https://docs.python.org/2/library/functions.html#open

Also, you may find it helpful to note that the literal string character of \\n will make a new line in a file (and if you want Windows-style new lines, you want \\r\\n - without that, opening in something like Notepad won't show you the new lines)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM