Yelp / dataset-examples

Samples for users of the Yelp Academic Dataset
http://www.yelp.com/academic_dataset
Other
1.23k stars 615 forks source link

json_to_csv_converter.py has a small issue for the new yelp dataset #42

Open akhodadadi opened 6 years ago

akhodadadi commented 6 years ago

For the file business.json, there are records (for example the 4th record) where for example the key hours exists but its value is None. This causes an exception in the function get_nested_value.

To fix it, I suggest to add the following to that function before the last line (return get_nested_value(sub_dict, sub_key)):

if sub_dict is None: return None

idkjs commented 6 years ago

@akhodadadi just came up on this. This is what worked for me.

def get_nested_value(d, key):
    """Return a dictionary item given a dictionary `d` and a flattened key from `get_column_names`.

    Example:

        d = {
            'a': {
                'b': 2,
                'c': 3,
                },
        }
        key = 'a.b'

        will return: 2

    """
    if '.' not in key:
        if key not in d:
            return None
        return d[key]
    base_key, sub_key = key.split('.', 1)
    if base_key not in d:
        return None
    sub_dict = d[base_key]
    if sub_dict is None:
        return None
    return get_nested_value(sub_dict, sub_key)

I know nothing about python so Thank You.