Skip to content

Commit 7b49edc

Browse files
committed
Simplify json-to-dict utility
1 parent 6d374c6 commit 7b49edc

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

tools/utils.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -337,25 +337,6 @@ def check_required_modules(required_modules, verbose=True):
337337
else:
338338
return True
339339

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-
359340
def json_file_to_dict(fname):
360341
""" Read a JSON file and return its Python representation, transforming all
361342
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):
366347
"""
367348
try:
368349
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)
371352
except (ValueError, IOError):
372353
sys.stderr.write("Error parsing '%s':\n" % fname)
373354
raise

0 commit comments

Comments
 (0)