1

I am logging in DataLogging.csvTemperature and Humidity:

2016-04-07 09:36    16.0    48.7
2016-04-07 09:37    16.0    48.7
2016-04-07 09:38    16.0    48.7
2016-04-07 09:39    16.1    48.8
2016-04-07 09:40    16.1    48.7

row[0] gets me the Date, row[1] the Temp and row[2] Humidity

What is the fastest way to get the Temperature and Humidity Values from the last row of the file(so those that have been logged last)? In this case the 09:40 values

@Edit: The Logger runs on a Raspberry Pi 2

I found this method: (Stackoverflow Link)

from collections import deque
import csv

def get_last_row(csv_filename):
    with open(csv_filename, 'rb') as f:
        return deque(csv.reader(f), 1)[0]

lastline = ', '.join(get_last_row('DataLogger.csv'))
values = lastline.split("\t")
print ((values[1]))

now I get the Temperature Value from the Last line ....

Community
  • 1
  • 1
Peter S
  • 625
  • 1
  • 9
  • 32
  • This problem is basically [`How to access the last line of a file`](http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail), plus a parsing of this last line to [access a particular column](http://stackoverflow.com/questions/17186893/accessing-column-data-from-a-csv-file-in-python). – aluriak Apr 07 '16 at 09:20

1 Answers1

2

I think you're on a Unix-y system so something simple like this should work + it's fast because it doesn't read the whole file:

import subprocess
last_line = subprocess.check_output(["tail", "-1", "DataLogging.csv"])
# to get the values (parsing via the csv module seems excessive for one line)
temp_hum = [float(x) for x in last_line.split()[2:]]

A cross-platform solution using seek() could be made by implemeting tails approach. A comment in tail's source code explains how it works:

Print the last N_LINES lines from the end of file FD. Go backward through the file, reading 'BUFSIZ' bytes at a time (except probably the first), until we hit the start of the file or have read NUMBER newlines. START_POS is the starting position of the read pointer for the file associated with FD (may be nonzero). END_POS is the file offset of EOF (one larger than offset of last byte). Return true if successful.

jDo
  • 3,962
  • 1
  • 11
  • 30
  • 1
    @PeterS Cool cool. I wonder if the `deque` solution will read the entire file from start to end. It might not be an issue now but if your file grows substantially, `tail`has the advantage of reading backwards. From [`tail`'s source code](http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c#n464): *"Go backward through the file, reading 'BUFSIZ' bytes at a time (except probably the first), until we hit the start of the file or have read NUMBER newlines."* – jDo Apr 07 '16 at 10:54
  • 1
    Yeah I'm not sure wether or not it reads through the whole file. For now it works pretty fast (20.000 lines in the file, about 2 Weeks measurements) I'd probably have to switch to you version when my pi just can't handle it anymore :-) Thank – Peter S Apr 07 '16 at 11:06