Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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))
Advertisement
Add Comment
Please, Sign In to add comment