pythonpython-3.xexceptionimdbimdbpy

how to handle empty movie language in imdbpy using python


This is my code, but it gives me error for movies that don't have language.i don't know how to handle the error.

from imdb import IMDb
ia = IMDb()
the_matrix = ia.get_movie(2234370)
the_matrix['language'] 

Error

 File "C:\ProgramData\Anaconda3\lib\site-packages\imdb\utils.py", line 1495, in __getitem__
    rawData = self.data[key]

KeyError: 'languages'

Solution

  • Use try except to handle the error!

    from imdb import IMDb
    ia = IMDb()
    the_matrix = ia.get_movie(2234370)
    try:
        the_matrix['language']
    except KeyError as ke:
        print(str(ke))
    

    or if it is a dictionary you can use this. the get(Key, None) method will return None if the key does not exists.

    from imdb import IMDb
    ia = IMDb()
    the_matrix = ia.get_movie(2234370)
    the_matrix.get('language', None)