First you must use json (or even ast.literal_eval ) instead of eval .
Secondly, this will not work, because as soon as you turn it into a regular dictionary, the whole order will be lost. You will need to parse "json" yourself if you want to put the information in an OrderedDict.
Fortunately, this is not as difficult as you think if you use the ast module. Here I assume that the dictionary contains only strings, but it should not be too difficult to modify for other purposes.
s = '{"id":"0","last_modified":"undefined"}' import ast from collections import OrderedDict class DictParser(ast.NodeVisitor): def visit_Dict(self,node): keys,values = node.keys,node.values keys = [ns for n in node.keys] values = [ns for n in node.values] self.od = OrderedDict(zip(keys,values)) dp = DictParser() dp.visit(ast.parse(s)) ordered_dict = dp.od print ordered_dict
source share