Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dict1 = {"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", "CASH": `"93200.0 "`,
- "CNRE": "28999", "PBGV": "1700"}, "sales_details": {"UTCC":
- "131619.0", "PBGV": "1700.0", "CASH": "92880.0", "CNRE": "28999.0"}},
- "275169": {"declaration_details": {"AMCC": "118616", "CNRE": "19462",
- "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"}}}}}
- def removew(d):
- for k, v in d.iteritems():
- if isinstance(v, dict):
- removew(v)
- else:
- d[k]=v.strip()
- removew(dict1)
- print dict1
- {'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'}}}}}
- def removew(d):
- return {k.strip():removew(v)
- if isinstance(v, dict)
- else v.strip()
- for k, v in d.iteritems()}
- removew(dict1)
- def strip_dict(d):
- return { key : strip_dict(value)
- if isinstance(value, dict)
- else value.strip()
- for key, value in d.items() }
- def strip_dict(d):
- """
- Recursively remove whitespace from keys and values in dictionary 'd'
- """
- for key, value in d.iteritems():
- if ' ' in key:
- d[key.strip()] = value
- del d[key]
- if isinstance(value, dict):
- strip_dict(value)
- elif isinstance(value, list):
- d[key.strip()] = [x.strip() for x in value]
- elif isinstance(value, str):
- d[key.strip()] = value.strip()
- def strip_dict(d):
- def strip_list(l):
- return [strip_dict(x)
- if isinstance(x, dict)
- else strip_list(x)
- if isinstance(x, list)
- else clean(value)
- for x in l]
- def clean(string):
- return ''.join(string.split())
- return { key.strip() : strip_dict(value)
- if isinstance(value, dict)
- else strip_list(value)
- if isinstance(value, list)
- else value
- if isinstance(value, bool)
- else clean(value)
- for key, value in d.items() }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement