PYTHON 45
QGIS Overwrite CSV - BG script By mike on 28th March 2021 03:44:13 PM
  1. #Read last line from large log files by bits and
  2. #savie it to a single line CSV to be monitored in QGIS
  3. #SeaIT - rev 190224b
  4.  
  5. # required
  6. import os
  7. import time
  8.  
  9. #The file to read from - use a small one to test in case it shits itself
  10. #readfile = "Z:/10030530 Borssele III&IV_SBP/21 NHP/21 Survey Online/01 [100XXXXX - Online Project]/LogFiles/Output to QGIS - Logfile 24Feb19.txt"
  11. readfile = "Z:/10030530 Borssele III&IV_SBP/21 NHP/21 Survey Online/Raw QGIS Input/QGIS Qinsy input.txt"
  12.  
  13. #Your CSV to write to - will be created if non existing
  14. writefile = "Z:/10030530 Borssele III&IV_SBP/21 NHP/29 Reporting/20 Ops QGIS/Ops QGIS Layers/07 SBP Phase/QGIS QINSy input - stripped.csv"
  15.  
  16. #Do the magic
  17. while True:
  18.   with open(readfile, 'rb') as fr:
  19.     fr.seek(-2, os.SEEK_END)
  20.     while fr.read(1) != b'\n':
  21.       fr.seek(-2, os.SEEK_CUR)
  22.     with open(writefile, 'w+') as fw:
  23.       fw.writelines(fr.readline().decode())
  24. #Print output in console
  25.           with open(writefile, 'r') as fw:     
  26.       first_line = fw.readline()
  27.       print(first_line)
  28.   time.sleep(30)

Hasta la pasta! is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.