我有一个脚本,可以将一些可怜的人从API返回的数据缓存为JSON对象的平面文件。一个结果/每行JSON对象。如何优化阅读和处理大文件?
缓存工作流程如下:
阅读在整个缓存文件 - >检查每一行是太旧了,一行行 - >保存是不是太旧到新列表中的 - >将新的新缓存列表打印到文件中,并将新列表用作筛选器,以便不针对API调用的传入数据。
到目前为止,这个过程的最长的部分是粗体上面。以下是代码:
print "Reading cache file into memory ---"
with open('cache', 'r') as f:
cache_lines = f.readlines()
print "Turning cache lines into json and checking if they are stale or not ---"
for line in cache_lines
# Load the line back up as a json object
try:
json_line = json.loads(line)
except Exception as e:
print e
# Get the delta to determine if data is stale.
delta = meta_dict["timestamp_start"] - parser.parse(json_line['timestamp_start'])
# If the data is still fresh then hold onto it
if cache_timeout >= delta:
fresh_cache.append(json_line)
根据散列文件的大小可能需要几分钟。有没有更快的方法来做到这一点?我理解,阅读整个文件并不理想,但最容易实现。
感谢您的意见。我希望有一些黑魔法,但看起来我运气不好。我会尽量不parser.parsing每个电话,看看是否有帮助。 – Thisisstackoverflow
你也可以尝试'simplejson'库,它比标准的'json'库更快... – ohe
好点。也是一个镜头。 – Thisisstackoverflow