The below code is from chapter 3 of Python Data Science Handbook by Jake VanderPlas. Each line in the file is a valid JSON. While I don't think the specifics of the file are critical to answering this question, the url for the file is https://github.com/fictivekin/openrecipes.
# read the entire file into a Python array
with open('recipeitems-latest.json', 'r') as f:
# Extract each line
data = (line.strip() for line in f)
# Reformat so each line is the element of a list
data_json = "[{0}]".format(','.join(data))
# read the result as a JSON
recipes = pd.read_json(data_json)
Two questions:
- why is a generator comprehension used rather than a list comprehension in the second line of the code? Since the desired final data structure is a list, I'm wondering why not work with only lists rather than working first with a generator and then a list?
- is it possible to use a list comprehension instead?
You have two questions in here:
Please see official documentation for more info.