|
yeah theres loads of possibilities depending on what your writing, how your reading etc.
If the pickup program uses python i’d wholeheartedly suggest using cPickle, by far the best method for this job, plus its fast.
try:
import cPickle as pickle
except:
import pickle
# nested DICTIONARY data = { 'one': {'label': 'This is shot 001', 'start': 1, 'end': 10},
'two': {'label': 'This is shot 002', 'start': 11, 'end': 25},
'three': {'label': 'This is shot 003 - Needs Editing', 'start': 26, 'end': 50} }
# write to file with cPickle/pickle (as binary) def ld_writeDicts(filePath,dict):
f=open(filePath,'w')
newData = pickle.dumps(dict, 1)
f.write(newData)
f.close()
ld_writeDicts('C:/Users/Lee/Desktop/test2.dta',data)
# read file decoding with cPickle/pickle (as binary) def ld_readDicts(filePath):
f=open(filePath,'r')
data = pickle.load(f)
f.close()
return data
# return dict data to new dict newDataDict = ld_readDicts('C:/Users/Lee/Desktop/test2.dta') # test nesting print newDataDict['one']['label']
Otherwise, if the data needs to be user-friendly (readable without decoding) i’d suggest json. (you could just dump the info as a raw string, but json will arrange and indent, looking prettier)
import json
# nested DICTIONARY data = { 'one': {'label': 'This is shot 001', 'start': 1, 'end': 10},
'two': {'label': 'This is shot 002', 'start': 11, 'end': 25},
'three': {'label': 'This is shot 003 - Needs Editing', 'start': 26, 'end': 50} }
# write to file with json encoding def ld_writeDicts(filePath,dict):
f=open(filePath,'w')
newData = json.dumps(dict, sort_keys=True, indent=4)
f.write(newData)
f.close()
ld_writeDicts('C:/test.dta',data)
# read file decoding with json def ld_readDicts(filePath):
f=open(filePath,'r')
data = json.loads(f.read())
f.close()
return data
# return dict data to new dict newDataDict = ld_readDicts('C:/test.dta') # test nesting print newDataDict['one']['label']
again I would suggest cPickle, as it’ll support far more elementary data types other than dictionaries.
try pressing 'b'…
ldunham.blogspot.com
|