【发布时间】:2017-12-06 08:35:07
【问题描述】:
我正在加载数据(20 个元素)到另一个文件加载...这里我想根据我在列表元素中使用的时间戳对这 20 个元素进行排序。
import json
from collections import OrderedDict
import datetime
import os
if os.path.exists("qwerty.json"):
record = json.load(open("qwerty.json", "r"), object_pairs_hook=OrderedDict)
else:
record = OrderedDict({})
fo = open("foo.txt", "wb")
abc = list(record.items())[:20]
print(abc)
command = ""
while command != 'exit':
command = input('Enter a command(options: create,read,save): ')
if command == "create":
name = input('Enter name of the Student:')
p = input('Student ID: ')
a = input('Class: ')
n = input('Marks: ')
time = datetime.datetime.now().isoformat()
record[name] = {'Student ID:': p, 'Class:': a, 'Marks': n, 'time': time }
elif command == 'read':
z = json.load(open("qwerty.json", "r"), object_pairs_hook=OrderedDict)
print(z)
elif command == 'save':
json.dump(record, open('qwerty.json', "w"))
fo.close()
【问题讨论】:
-
你的问题是……?
标签: python json file caching lru