1

Obviously, this is a question a beginner would ask. However, I am struggling adding keys to my dictionary to correct the following errors:

import csv, json, sys

def find_deep_value(d, key):
# Modified from https://stackoverflow.com/questions/48568649/convert-json-to-csv-using-python/48569129#48569129

    if key in d:
        return d[key]
    for k in d.keys():
        if isinstance(d[k], dict):
            for j in find_deep_value(d[k], key):
                return j

inputFile = open("pywu.cache.json", 'r')  # open json file
outputFile = open("CurrentObs.csv", 'w')  # load csv file
data = json.load(inputFile)  # load json content
inputFile.close()  # close the input file
output = csv.writer(outputFile)  # create a csv.write

# Gives you latitude coordinates from within the json
lat = find_deep_value(data, "latitude")

# Gives you longitude coordinates from within the json
lon = find_deep_value(data, "longitude")

# Gives you a list of weather from within the json
weather = find_deep_value(data, "weather")

# Gives you a list of temperature_strings from within the json
temp = find_deep_value(data, "temperature_string")

output.writerow([lat, lon, weather, temp])

returns the error, below:

outputFile.close()
  File "json_to_csv.py", line 20, in <module>
    lat = find_deep_value(data, "latitude")
  File "json_to_csv.py", line 10, in find_deep_value
    for j in find_deep_value(d[k], key):
  File "json_to_csv.py", line 10, in find_deep_value
    for j in find_deep_value(d[k], key):
TypeError: 'NoneType' object is not iterable

How would I go about fixing this? I've tried creating an empty dictionary "dict={}" and adding that way, but still returns nothing.

All help is appreciated!

3
  • 2
    You need to provide a minimal reproducible example Commented Feb 2, 2018 at 21:50
  • I am a little bit offf topic here but if what you want is to convert a Json to csv, have you considered using pandas library? It has a method which does just that in one line. Commented Feb 2, 2018 at 21:55
  • I was able to run the python command, "dict = dict.keys(['latitude', 'longitude', 'weather', 'temp']), which appears to have added the appropriate keys. However, the script is only returning the first letter/ character of a value. Commented Feb 2, 2018 at 22:28

1 Answer 1

1

You also need to handle the case if nothing is found. Therefore, return an empty dict instead of None, i.e. having no return that is executed:

def find_deep_value(d, key):
    if key in d:
        return d[key]
    for k in d.keys():
        if isinstance(d[k], dict):
            for j in find_deep_value(d[k], key):
                return j
    return {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.