Python Forum
Fastest Way of Writing/Reading Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fastest Way of Writing/Reading Data
#1
I have inherited a script that scrapes data from the web every few seconds and stores it in a mysql database. It then deletes data that is more than 30 seconds old. This means there is a maximum of 5-6 records in the database. Each record consists of 2 fields (scrape time and the data).

I don't know why mysql has been used to store the data but it seems unnecessarily cumbersome (although I am a beginner).

Is my thinking correct? If so, what would be the quickest way to write/read/clear data in these circumstances?
Reply
#2
you can use a flat file for storage, open using mode 'w+', ('a+' if you wish to keep old contents) and after each write make a call to flush()

example -- something like this (won't run as written, but syntax is there):
import os from pathlib import Path # following assures starting directory same as script. os.chdir(os.path.abspath(os.path.dirname(__file__))) # create ref to script directory homepath = Path('.') # Here, create a subdirectory to hold output data (move this to wherever you wish) # if paths exists, do nothing. If not create them OutDatapath = homepath / 'data_out' OutDatapath.mkdir(exist_ok=True) outfile = OutDatapath / 'MyData.txt' # Rename this as you wish with outfile.open('w+') as fp: # ... your code here fp.write(scraped_data) fp.flush()
since the file is flushed each time written, it can be opened for read by another program, and get all data up to the last write.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 733 Oct-11-2025, 11:25 PM
Last Post: Pedroski55
  Reading an ASCII text file and parsing data... oradba4u 2 2,801 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 2,292 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Fastest way tkinter Quatrixouuu 2 1,869 Feb-19-2024, 07:20 AM
Last Post: Danishhafeez
  What is the fastest way to get all the frames from a video file? glorsh66 3 7,913 May-26-2023, 04:41 AM
Last Post: Gribouillis
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 7,245 Dec-06-2022, 11:09 AM
Last Post: mg24
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 4,382 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Create a function for writing to SQL data to csv mg24 4 3,492 Oct-01-2022, 04:30 AM
Last Post: mg24
  Reading Data from JSON tpolim008 2 3,939 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Need Help writing data into Excel format ajitnayak87 8 5,180 Feb-04-2022, 03:00 AM
Last Post: Jeff_t

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.