I have a json file that is being updated regularly from another python script.
I want to read the most recently appended data in that json file. In other words, I want to read JSON file in a tail -f
manner.
I know how to do this in a text file:
self.fileName = fileName
self.file = open(self.fileName, 'r')
self.st_results = os.stat(fileName)
self.st_size = self.st_results[6]
self.file.seek(self.st_size)
while 1:
where = self.file.tell()
line = self.file.readline()
if not line:
print "No line waiting, waiting for one second"
time.sleep(1)
self.file.seek(where)
else:
print line
I have tried this with json but it didn't work. Probably because readline is not appropriate.
For eg: My json:
{
"created_at": "2018-05-05 06:57:58",
"id": 992659653782798338,
"sequence": 7,
"tweet": "RT @SirJadeja: Fun Fact: Stoinis Hit Hardik Pandya For 20 Runs In His Last Over. Brother Krunal Pandya(15) Together With Rohit Sharma(5) Hi\u2026"
}{
"created_at": "2018-05-05 06:58:00",
"id": 992659660204208128,
"sequence": 8,
"tweet": "RT @surya_14kumar: A very important victory, achieved through a collective team effort. This will definitely set the momentum for us going\u2026"
}
I start my raedTweets.py script which should print the lastly appended data.
{
"created_at": "2018-05-05 06:59:43",
"id": 992660091508699137,
"sequence": 13,
"tweet": "RT @AndhraPolls: Even a simple inquiry would put #TDP leadership behind the bars.\n#AndhraPradesh #FridayFeeling #MahilaParaBJPSarkara #BJPV\u2026"
}
The output should be:
{ "created_at": "2018-05-05 06:59:43", "id": 992660091508699137, "sequence": 13, "tweet": "RT @AndhraPolls: Even a simple inquiry would put #TDP leadership behind the bars.\n#AndhraPradesh #FridayFeeling
MahilaParaBJPSarkara #BJPV\u2026" }