Guest User

Untitled

a guest
Oct 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. d = """{
  2. "motion_measure": {"INCAR": 69, "RANDOM": 63, "UNKNOWN": 62, "BIKING": 57, "WALKING": 48, "RUNNING": 41, "SEDENTARY": 0},
  3. "samples": [0, 1.1791444, 11.036073],
  4. "record_time": datetime.datetime(2018, 3, 26, 10, 3, 17, 441000)
  5. }"""
  6.  
  7. import ast
  8.  
  9. ast.literal_eval(d)
  10.  
  11. import json
  12.  
  13. json.loads(d)
  14.  
  15. import datetime
  16. d = <... your string ...>
  17. new_dict = eval(d)
  18.  
  19. import datetime
  20. import re
  21. import json
  22.  
  23. s = """{
  24. "motion_measure": {"INCAR": 69, "RANDOM": 63, "UNKNOWN": 62, "BIKING": 57, "WALKING": 48, "RUNNING": 41, "SEDENTARY": 0},
  25. "samples": [0, 1.1791444, 11.036073],
  26. "record_time": datetime.datetime(2018, 3, 26, 10, 3, 17, 441000),
  27. "another_time": datetime.datetime(2017, 3, 26, 10, 3, 17, 441000)
  28. }"""
  29.  
  30. # re.sub to replace datetime; json.loads to convert to dict
  31. d = json.loads(re.sub(r'datetime.datetime(([^)]*))', r'"1"', s))
  32.  
  33. # datetime.datetime.strptime(...) to work with resulting datetime strings
  34. date = datetime.datetime.strptime(d['record_time'], '%Y, %m, %d, %H, %M, %S, %f')
  35.  
  36. print(d)
  37. # OUTPUT (shown on multiple lines for readability)
  38. # {
  39. # 'motion_measure': {'INCAR': 69, 'RANDOM': 63, 'UNKNOWN': 62, 'BIKING': 57, 'WALKING': 48, 'RUNNING': 41, 'SEDENTARY': 0},
  40. # 'samples': [0, 1.1791444, 11.036073],
  41. # 'record_time': '2018, 3, 26, 10, 3, 17, 441000',
  42. # 'another_time': '2017, 3, 26, 10, 3, 17, 441000'
  43. # }
  44.  
  45. print(date)
  46. # OUTPUT
  47. # 2018-03-26 10:03:17.441000
Add Comment
Please, Sign In to add comment