0

I have a txt file which contains json string on each line with first line like this:

{"rating": 9.3, "genres": ["Crime", "Drama"], "rated": "R", "filming_locations": "Ashland, Ohio, USA", "language": ["English"], "title": "The Shawshank Redemption", "runtime": ["142 min"], "poster": "http://img3.douban.com/lpic/s1311361.jpg", "imdb_url": "http://www.imdb.com/title/tt0111161/", "writers": ["Stephen King", "Frank Darabont"], "imdb_id": "tt0111161", "directors": ["Frank Darabont"], "rating_count": 894012, "actors": ["Tim Robbins", "Morgan Freeman", "Bob Gunton", "William Sadler", "Clancy Brown", "Gil Bellows", "Mark Rolston", "James Whitmore", "Jeffrey DeMunn", "Larry Brandenburg", "Neil Giuntoli", "Brian Libby", "David Proval", "Joseph Ragno", "Jude Ciccolella"], "plot_simple": "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.", "year": 1994, "country": ["USA"], "type": "M", "release_date": 19941014, "also_known_as": ["Die Verurteilten"]}

I want to get data for imdb_id and title.

I have tried:

import json
data = json.load('movie_acotrs_data.txt')

But got 'str' object has no attribute 'read'

What should I do to get the result I expected?

0

2 Answers 2

3

json.load() expects the file to be just one long JSON string. You can't use it if it's a separate JSON string on each line. You need to read each line and call json.loads().

import json
with open('movie_actors_data.txt') as f:
    data = list(map(json.loads, f))

data will be a list of dictionaries.

If you just want a few properties, you can use a list comprehension.

with open('movie_actors_data.txt') as f:
    data = [{"title": x["title"], "imdb_id": x["imdb_id"]} for x in map(json.loads, f)]
Sign up to request clarification or add additional context in comments.

3 Comments

Probably cleaner as a list comprehension
Thanks! It parses all the data. How could I only get the imdb_id and title from here?
Added a list comprehension that selects specific properties.
1

json.load takes an open file, not a file path.

data = json.load(open('movie_acotrs_data.txt'))

3 Comments

Better within a with block I suppose.
I tried it and the result says: JSONDecodeError: Extra data: line 2 column 1 (char 936)
This doesn't work with multiple JSON strings in the file.

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.