[英]Remove json objects based on key value using Python
編輯:忘了提我正在使用 Python 2.7
我有一個大的 json 文件,結構如下:
[{
"headline": "Algérie Télécom prolonge son dispositif spécial Covid-19",
"url_src": "https://www.algerie360.com/algerie-telecom-prolonge-son-dispositif-special-covid-19/",
"img_src": "https://www.algerie360.com/wp-content/uploads/2020/04/DIA-Iddom-Algérie-télécom-320x200.jpg",
"news_src": "Algérie 360",
"catPT": "Ciência e Tecnologia",
"catFR": "Science et Technologie",
"catEN": "Science and Technology",
"lang": "French",
"epoch": 1591293345.817
},
{
"headline": "Internet haut débit à Alger : Lancement de la généralisation du » fibre to home »",
"url_src": "https://www.algerie360.com/20200510-internet-haut-debit-a-alger-lancement-de-la-generalisation-du-fibre-to-home/",
"img_src": "https://www.algerie360.com/wp-content/uploads/2020/05/unnamed-320x200.jpg",
"news_src": "Algérie 360",
"catPT": "Ciência e Tecnologia",
"catFR": "Science et Technologie",
"catEN": "Science and Technology",
"lang": "French",
"epoch": 1591283345.817
},
...
我一直在嘗試編寫一個.py 腳本來打開我的 json 文件,刪除“epoch”鍵小於 1591293345.817 的所有對象,並覆蓋當前文件。
這可能嗎?
我嘗試了以下方法,但我的 python 知識充其量是粗略的:
import time
import os
import json
import jsonlines
json_lines = []
with open('./json/news_done.json', 'r') as open_file:
for line in open_file.readlines():
j = json.loads(line)
now = time.time()
print(j['epoch'])
lastWeek = now - 3600
if not j['{epoch}'] > lastWeek:
json_lines.append(line)
with open('./json/news_done.json', 'w') as open_file:
open_file.writelines('\n'.join(json_lines))
您是否嘗試過 pandas 框架? 您可以使用它輕松過濾列。
我得到了這個代碼片段與您的示例數據一起使用:
import pandas as pd
import json
dataset = pd.read_json('example.json')
new_dataset = dataset[dataset['epoch'] >= 1591293345.817]
final_data = new_dataset.to_json(orient='records')
with open('example.json', 'w') as f:
json.dump(final_data, f)
看起來你只是刪除了“epoch”標簽,但如果我理解正確,你想解雇整個元素
您可以將文件完全打開為 json 而不是單獨的行
import json,time
with open('./json/news_done.json', 'r') as open_file:
yourFileRead = open_file.read()
yourJson = json.loads(yourFileRead)
filteredList = []
for j in yourJson: # j is one element out of the list not only one line
if time.time()-3600 > j['epoch']:
filteredList.append(j)
with open('./json/news_done.json', 'w') as open_file:
open_file.write(json.dumps(filteredList))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.