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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
iilla
  • 105
  • 7

2 Answers2

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)]
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

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

data = json.load(open('movie_acotrs_data.txt'))
scnerd
  • 5,836
  • 2
  • 21
  • 36