Reputation: 195
I am getting the error as described in the title when calling the function:
def read_file(file_name):
"""Return all the followers of a user."""
f = open(file_name, 'r')
data = []
for line in f:
data.append(json.loads(line.strip()))
f.close()
return data
Sample data looks like this:
"from": {"name": "Ronaldo Naz\u00e1rio", "id": "618977411494412"},
"message": "The king of football. Happy birthday, Pel\u00e9!",
"type": "photo", "shares": {"count": 2585}, "created_time": "2018-10-23T11:43:49+0000",
"link": "https://www.facebook.com/ronaldo/photos/a.661211307271022/2095750393817099/?type=3",
"status_type": "added_photos",
"reactions": {"data": [], "summary": {"total_count": 51779, "viewer_reaction": "NONE"}},
"comments": {"data": [{"created_time": "2018-10-23T11:51:57+0000", ... }]}
Upvotes: 1
Views: 7274
Reputation: 81604
You are trying to parse each line of the file as JSON on its own, which is probably wrong. You should read the entire file and convert to JSON at once, preferably using with
so Python can handle the opening and closing of the file, even if an exception occures.
The entire thing can be condensed to 2 lines thanks to json.load
accepting a file object and handling the reading of it on its own.
def read_file(file_name):
with open(file_name) as f:
return json.load(f)
Upvotes: 2