Created
July 21, 2016 19:49
-
-
Save jhyland87/03d2a75d5eee027ef35ac1b24274ee83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = [ | |
{ | |
"reason":{ | |
"inventory_event_reason":"10", | |
"reason_input_36":"see david's email", | |
"reason_input_33":"", | |
"reason_input_34":"Credit", | |
"reason_input_30":"Wayne\u2019s Candies", | |
"reason_input_31":"", | |
"reason_input_35":"", | |
"reason_input_29":"", | |
"reason_input_32":"" | |
}, | |
"items":[ | |
{ | |
"ITEMID":"112996", | |
"NAME":"TSC CROWN PEANUT LOG 3OZ 12CT-429", | |
"LOCATION":"TN", | |
"ONHAND":"224", | |
"box_cost":"7.2", | |
"item_status":"ACTIVE", | |
"ADJUSTMENTTYPE":"adjustment", | |
"ADJUSTMENT":"180" | |
} | |
] | |
}, | |
"jmatlock", | |
"10", | |
"Jeffrey Matlock <[email protected]>" | |
] | |
def sanatize( data ): | |
def _sanatizeStr( dirty_string ): | |
chars = { | |
u'\u201c': '"', # RIGHT DOUBLE QUOTATION MARK | |
u'\u201d': '"', # RIGHT DOUBLE QUOTATION MARK | |
u'\u2018': "'", # LEFT SINGLE QUOTATION MARK | |
u'\u2019': "'", # RIGHT SINGLE QUOTATION MARK | |
u'\u2014': "-", # EM DASH | |
u'\u2013': "-", # EM DASH | |
u'\u02DC': "~", # SMALL TILDE | |
u'\u201A': "'", # SINGLE LOW-9 QUOTATION MARK | |
u'\u201E': '"' # DOUBLE LOW-9 QUOTATION MARK | |
} | |
dirty_string = dirty_string.translate(None, string.punctuation).upper() | |
for k, v in chars.iteritems(): | |
dirty_string = dirty_string.replace(k, v) | |
return dirty_string | |
def _sanatizeList( listData ): | |
for subkey, subvalue in enumerate(listData): | |
#print("Subkey %s, Subvalue %s" % (subkey, subvalue)) | |
if isinstance( subvalue, str ): | |
listData[subkey] = _sanatizeStr( subvalue ) | |
else: | |
listData[subkey] = sanatize( subvalue ) | |
return listData | |
def _sanatizeDict( dictData ): | |
for key, value in dictData.items(): | |
#print("Parsing key %s, value %s" % (key, value)) | |
if isinstance( value, str ): | |
dictData[key] = _sanatizeStr( value ) | |
else: | |
dictData[key] = sanatize( value ) | |
return dictData | |
if isinstance( data, list ): | |
return _sanatizeList( data ) | |
if isinstance( data, dict ): | |
return _sanatizeDict( data ) | |
if isinstance( data, str ): | |
return _sanatizeStr( data ) | |
return data | |
print "Before:" | |
print data | |
data = sanatize( data ) | |
print "After:" | |
print data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment