@@ -337,25 +337,6 @@ def check_required_modules(required_modules, verbose=True):
337
337
else :
338
338
return True
339
339
340
- def dict_to_ascii (dictionary ):
341
- """ Utility function: traverse a dictionary and change all the strings in
342
- the dictionary to ASCII from Unicode. Useful when reading ASCII JSON data,
343
- because the JSON decoder always returns Unicode string. Based on
344
- http://stackoverflow.com/a/13105359
345
-
346
- Positional arguments:
347
- dictionary - The dict that contains some Unicode that should be ASCII
348
- """
349
- if isinstance (dictionary , dict ):
350
- return OrderedDict ([(dict_to_ascii (key ), dict_to_ascii (value ))
351
- for key , value in dictionary .items ()])
352
- elif isinstance (dictionary , list ):
353
- return [dict_to_ascii (element ) for element in dictionary ]
354
- elif isinstance (dictionary , unicode ):
355
- return dictionary .encode ('ascii' ).decode ()
356
- else :
357
- return dictionary
358
-
359
340
def json_file_to_dict (fname ):
360
341
""" Read a JSON file and return its Python representation, transforming all
361
342
the strings from Unicode to ASCII. The order of keys in the JSON file is
@@ -366,8 +347,8 @@ def json_file_to_dict(fname):
366
347
"""
367
348
try :
368
349
with open (fname , "r" ) as file_obj :
369
- return dict_to_ascii ( json .load (file_obj ,
370
- object_pairs_hook = OrderedDict ) )
350
+ return json .loads (file_obj . read (). encode ( 'ascii' , 'ignore' ) ,
351
+ object_pairs_hook = OrderedDict )
371
352
except (ValueError , IOError ):
372
353
sys .stderr .write ("Error parsing '%s':\n " % fname )
373
354
raise
0 commit comments