How to load json data from serialised file and process them in python 3? -
i trying create program in python3 (mac os x) , tkinter. takes incremental id, datetime.now , third string variables. example,
a window opens displaying : id / date time / "hello world". user makes choice , presses save button. inputs being serialised json , saved in file.
mytest = dict([('testid',testid), ('testdate',testdate), ('teststyle',teststyle)]) open('data/test.txt', mode = 'a', encoding = 'utf-8') myfile: json.dump(mytest, myfile, indent = 2) myfile.close() the result in file is
{ "teststyle": "blabla", "testid": "8", "testdate": "2013-05-09 13:32" }{ "testdate": "2013-05-09 13:41", "testid": "9", "teststyle": "blabla" } as python newbie, want load file data , make checks, "if user made entry @ 2013-05-09, display message saying entered data today." proper way load these json data ? list expand each day , contain lots of data.
instead of directly storing dictionary can store list of dictionaries can loaded list can modified , appended
import json mytest1 = dict([('testid','testid1'), ('testdate','testdate1'), ('teststyle','teststyle1')]) json_values = [] json_values.append(mytest1) s = json.dumps(json_values) print(s) json_values = none mytest2 = dict([('testid','testid2'), ('testdate','testdate2'), ('teststyle','teststyle2')]) json_values = json.loads(s) json_values.append(mytest2) s = json.dumps(json_values) print(s)
Comments
Post a Comment