Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. dict1 = {"1434": {"2012-10-29": {"275174": {"declaration_details":
  2. {"UTCC": `"38483 "`, "CNRE": "8334", "CASH": "55096.0"},
  3. "sales_details": {"UTCC": "38483.0", "CNRE": "8334.0", "CASH":
  4. "55098.0"}}, "275126": {"declaration_details": {"CNIS": "63371"},
  5. "sales_details": {"CNIS": "63371.0"}}, "275176":
  6. {"declaration_details": {"UTCC": "129909", "CASH": `"93200.0 "`,
  7. "CNRE": "28999", "PBGV": "1700"}, "sales_details": {"UTCC":
  8. "131619.0", "PBGV": "1700.0", "CASH": "92880.0", "CNRE": "28999.0"}},
  9. "275169": {"declaration_details": {"AMCC": "118616", "CNRE": "19462",
  10. "CASH": "120678.0"}, "sales_details": {"UTCC": "118616.0", "CNRE":
  11. "19462.0", "CASH": "120677.0"}}, "266741": {"declaration_details":
  12. {"UTCC": "42678", "CNRE": "4119", "CASH": `"24944.0 "`},
  13. "sales_details": {"UTCC": "42678.0", "CNRE": "4119.0", "CASH":
  14. "24944.0"}}}}}
  15.  
  16. def removew(d):
  17. for k, v in d.iteritems():
  18. if isinstance(v, dict):
  19. removew(v)
  20. else:
  21. d[k]=v.strip()
  22.  
  23.  
  24. removew(dict1)
  25. print dict1
  26.  
  27. {'1434': {'2012-10-29': {'275174': {'declaration_details': {'UTCC': '38483', 'CNRE': '8334', 'CASH': '55096.0'}, 'sales_details': {'UTCC': '38483.0', 'CNRE': '8334.0', 'CASH': '55098.0'}}, '275126': {'declaration_details': {'CNIS': '63371'}, 'sales_details': {'CNIS': '63371.0'}}, '275176': {'declaration_details': {'UTCC': '129909', 'CNRE': '28999', 'CASH': '93200.0', 'PBGV': '1700'}, 'sales_details': {'UTCC': '131619.0', 'CNRE': '28999.0', 'CASH': '92880.0', 'PBGV': '1700.0'}}, '275169': {'declaration_details': {'CNRE': '19462', 'AMCC': '118616', 'CASH': '120678.0'}, 'sales_details': {'UTCC': '118616.0', 'CNRE': '19462.0', 'CASH': '120677.0'}}, '266741': {'declaration_details': {'UTCC': '42678', 'CNRE': '4119', 'CASH': '24944.0'}, 'sales_details': {'UTCC': '42678.0', 'CNRE': '4119.0', 'CASH': '24944.0'}}}}}
  28.  
  29. def removew(d):
  30. return {k.strip():removew(v)
  31. if isinstance(v, dict)
  32. else v.strip()
  33. for k, v in d.iteritems()}
  34. removew(dict1)
  35.  
  36. def strip_dict(d):
  37. return { key : strip_dict(value)
  38. if isinstance(value, dict)
  39. else value.strip()
  40. for key, value in d.items() }
  41.  
  42. def strip_dict(d):
  43. """
  44. Recursively remove whitespace from keys and values in dictionary 'd'
  45. """
  46.  
  47. for key, value in d.iteritems():
  48. if ' ' in key:
  49. d[key.strip()] = value
  50. del d[key]
  51. if isinstance(value, dict):
  52. strip_dict(value)
  53. elif isinstance(value, list):
  54. d[key.strip()] = [x.strip() for x in value]
  55. elif isinstance(value, str):
  56. d[key.strip()] = value.strip()
  57.  
  58. def strip_dict(d):
  59. def strip_list(l):
  60. return [strip_dict(x)
  61. if isinstance(x, dict)
  62. else strip_list(x)
  63. if isinstance(x, list)
  64. else clean(value)
  65. for x in l]
  66.  
  67. def clean(string):
  68. return ''.join(string.split())
  69.  
  70. return { key.strip() : strip_dict(value)
  71. if isinstance(value, dict)
  72. else strip_list(value)
  73. if isinstance(value, list)
  74. else value
  75. if isinstance(value, bool)
  76. else clean(value)
  77. for key, value in d.items() }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement