2

I have a program that takes voltages vs. time which is put into a file, 'logfile.txt'. Later the logfile is used to make a plot in Pylab. The program works fine except that the very first line in the logfile needs to be deleted before plotting. I don't know why, but the first line contains the last voltage from the previous test, even though the logfile shows empty before the start of each new test. For example here is typical of first 4 lines in logfile:

     1379812114.42 2.056

     1379812129.0 2.130

     1379812129.22 2.252

     1379812129.45 2.266

If I could just keep that first line out of the logfile and therefore out of the Pylab plot I'd be happy. That 2.056 is from a previous test and shouldn't be there. Here are the pertinent lines in the program:

 with open('logfile.txt', 'a') as f:
     while True:
        volts = adc.readADCDifferential01(4096, 8)
        sys.stdout.flush()
        if volts >=2.0:
            print >> f, time(), '{:.1f}'.format(volts)
            sleep(0.1)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rico
  • 329
  • 3
  • 9
  • 2
    You're opening the file in append mode... If the entirety of what you're writing to it is inside the `with` block... then you (possibly) don't need that - just open it with a `w` instead – Jon Clements Dec 15 '13 at 00:41
  • I changed to "with open('logfile.txt', 'w') as f:" But it didn't help, the last reading from previous test still made it into new logfile. It's like my analog to digital converter has a small memory of it's own. I think the first line of the logfile needs to be deleted. – Rico Dec 15 '13 at 01:01
  • Also confused why you're opening the logfile after the `pylab.show()` in write mode – Jon Clements Dec 15 '13 at 01:04
  • I think I did that to delete the logfile.txt that was just plotted, so that it's empty for the next test. – Rico Dec 15 '13 at 01:09
  • 1
    Nope - not enough code here to be going on with - it's going to a silly debugging thing by the looks of it... you're going to need to provide more context (surrounding code) I think... – Jon Clements Dec 15 '13 at 01:21
  • I added on to my program above. – Rico Dec 15 '13 at 01:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43197/discussion-between-rico-and-jon-clements) – Rico Dec 15 '13 at 03:26

2 Answers2

1

In addition to writing a new file, you can use numpy's capability to skip rows from the start and end of a file using np.genfromtxt. Specifically the arguments you are looking for include:

skip_header : int, optional

The numbers of lines to skip at the beginning of the file.

skip_footer : int, optional

The numbers of lines to skip at the end of the file

Since you are using pylab, you will be using numpy implicitly anyways.

Community
  • 1
  • 1
Hooked
  • 84,485
  • 43
  • 192
  • 261
0

Thanks to an old post from SilentGhost:

lines = open('logfile.txt').readlines() 
open('newfile.txt', 'w').writelines(lines[1:-1])

This will create a new file from the original file, except the new file will not have the first or last lines of the original file. The new file can then be used to make the plot.

Rico
  • 329
  • 3
  • 9
  • 1
    This "trick" is called _slicing_ in Python and it's very powerful. See http://stackoverflow.com/questions/509211/pythons-slice-notation for a good overview. – Hooked Dec 16 '13 at 14:09
  • Thank you Hooked, I'll save that for reference. – Rico Dec 16 '13 at 14:40