
Untitled
By: a guest on
Jul 14th, 2012 | syntax:
None | size: 0.65 KB | hits: 15 | expires: Never
string to OrderedDict conversion in python
OrderedDict([(7, 0), (6, 1), (5, 2), (4, 3)])
myfile = open('filename.txt','r')
mydict = myfile.read()
<class 'collections.OrderedDict'>
import cPickle as pickle
# store:
with open("filename.pickle", "w") as fp:
pickle.dump(ordered_dict, fp)
# read:
with open("filename.pickle") as fp:
ordered_dict = pickle.load(fp)
type(ordered_dict) # <class 'collections.OrderedDict'>
import re
import ast
import collections
with open(filename.txt) as file:
line = next(file)
values = re.search(r"OrderedDict((.*))", line).group(1)
mydict = collections.OrderedDict(ast.literal_eval(values))