code_junkie

Python human readable object serialization

Nov 13th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. >>> d = {'age': 27,
  2. ... 'name': 'Joe',
  3. ... 'numbers': [1,
  4. ... 2,
  5. ... 3,
  6. ... 4,
  7. ... 5],
  8. ... 'subdict': {
  9. ... 'first': 1,
  10. ... 'second': 2,
  11. ... 'third': 3
  12. ... }
  13. ... }
  14. >>>
  15. >>> from pprint import pprint
  16. >>> pprint(d)
  17. {'age': 27,
  18. 'name': 'Joe',
  19. 'numbers': [1, 2, 3, 4, 5],
  20. 'subdict': {'first': 1, 'second': 2, 'third': 3}}
  21. >>>
  22.  
  23. In [109]: simplejson.loads(simplejson.dumps({'d':(12,3,4,4,5)}))
  24. Out[109]: {u'd': [12, 3, 4, 4, 5]}
  25.  
  26. import simplejson
  27. my_structure = {"name":"Joe", "age":27, "numbers":[1,2,3,4,5], "subdict":{"first":1, "second":2, "third": 3}}
  28. json = simplejson.dumps(my_structure)
  29.  
  30. {"age": 27, "subdict": {"second": 2, "third": 3, "first": 1}, "name": "Joe", "numbers": [1, 2, 3, 4, 5]}
  31.  
  32. import pprint
  33. pprint.pprint(my_structure)
  34.  
  35. {'age': 27,
  36. 'name': 'Joe',
  37. 'numbers': [1, 2, 3, 4, 5],
  38. 'subdict': {'first': 1, 'second': 2, 'third': 3}}
  39.  
  40. {'age': 27,
  41. 'name': 'Joe',
  42. 'numbers': [1,
  43. 2,
  44. 3,
  45. 4,
  46. 5],
  47. 'subdict': {
  48. 'first': 1,
  49. 'second': 2,
  50. 'third': 3
  51. }
  52. }
Add Comment
Please, Sign In to add comment