str() turns data into strings but is it possible to undo this with a built in function? exe:
str1 = '{"a": 1, "b": 2}' is there a function that can turn this into a dictionary? if not what can I do?
I tried:
dict1 = dict(str1) but I only just found out that it's not the way to do it
ast.literal_eval().str(), usejson.dumps(), which you can reverse withjson.loads(). Thestr()method isn't designed to be reversible (evalwill work on it sometimes but not always), whereas JSON encoding is specifically designed to be. (There are other encoding methods, but JSON is a very widely used one and will work great with the type of data you're talking about. It also happens to look a lot like what you get fromstr()but it's not exactly the same.)datetime.datetime, which provides the instance methodstrftimeto produce a string and the class methodstrptimeto parse a string.