我有一个json文件正在从另一个python脚本定期更新。
我想读取该json文件中最近附加的数据。换句话说,我想以tail -f
的方式阅读JSON文件。
我知道如何在文本文件中执行此操作:
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
我和json一起尝试了这个但是它没有用。可能是因为readline不合适。
例如: 我的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"
}
我启动了raedTweets.py脚本,该脚本应该打印最后附加的数据。
{
"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"
}
输出应为:
{ " created_at":" 2018-05-05 06:59:43", " id":992660091508699137, "序列":13, "鸣叫":" RT @AndhraPolls:即使是简单的调查也会让#TDP领导层落后于酒吧。\ n#AndhraPradesh #FridayFeeling
MahilaParaBJPSarkara #BJPV \ u2026" }
答案 0 :(得分:0)
如果要求是以某种格式(键,值)保存数据,则应尝试使用csv而不是json。我有一个解决方法,以tail -f方式读取csv文件,这解决了我的问题。 This回答提供了完美的解决方案。