[Tutor] Only appending one object to list, when I am expecting more than 1

Peter Otten __peter__ at web.de
Tue Feb 26 04:39:02 EST 2019


AdamC wrote:

> I'm creating lots of objects from json in a file. Part of reading the json
> back means that it iterates over the file and loads a json object and then
> creates the object from the dictionary.
> 
> This is my  file:
> 
> {"name": "Dwarf Fortress", "platform": "steam", "dateAdded":
> "2019:02:25:16:56:1551113768", "tpe": "2019:02:21:13:49:1550756942"}
> {"name": "Jaws", "platform": "Netflix", "dateAdded":
> "2019:02:25:16:56:1551113768", "tpe": "2019:02:21:13:49:1550756960"}
> {"name": "Wargames", "platform": "CLI", "dateAdded":
> "2019:02:25:16:59:1551113984", "tpe": "Game"}
> 
> and these are the functions that help load that file:
> 
> media = []
> 
> def loadFile():
>     filename = input('Filename? ')
>     f = open(filename, 'r')
>     createObjects(f)
> 
> def createObjects(f):
>     '''Takes a file object and iterates through entries, passing them to
> create
>     object, depending on what object it is.'''
>     for line in f:
>         count = count + 1
>         data = json.loads(line)
>         print(type(data['tpe']))
>         name = data['name']
>         platform = data['platform']
>         dateAdded = data['dateAdded']
>         tpe = data['tpe']
>         if data['tpe'] == 'Game':
>             a = createGame(name, platform, dateAdded,tpe)
>             game = {a: tpe}
>             media.append(game)
>         if data['tpe'] == 'Film':
>             a = createFilm(name, platform, dateAdded,tpe)
>             film = {a: tpe}
>             media.append(film)
>     # For some reason I'm only getting one object at a time now when
> appending to media
>     print(len(media))
> 
> def createGame(name, platform, dateAdded, tpe):
>     return Game(name, platform, dateAdded)
> 
> def createFilm(name, platform, dateAdded, tpe):
>     return Film(name, platform, dateAdded)
> 
> (This isn't the order that the functions are held in the module).
> Why would I only get one object in media, even though all three are
> created?

Only one record in your jsonl sample has a 'tpe' handled by createObjects(). 
To make it easier to catch this problem (invalid or unsuspected data) I 
suggest that you rewrite the

>         if data['tpe'] == 'Game':
>             a = createGame(name, platform, dateAdded,tpe)
>             game = {a: tpe}
>             media.append(game)
>         if data['tpe'] == 'Film':
>             a = createFilm(name, platform, dateAdded,tpe)
>             film = {a: tpe}
>             media.append(film)

part of your code as

        if data['tpe'] == 'Game':
            a = createGame(name, platform, dateAdded,tpe)
            game = {a: tpe}
            media.append(game)
        elif data['tpe'] == 'Film':
            a = createFilm(name, platform, dateAdded,tpe)
            film = {a: tpe}
            media.append(film)
        else:
            # Instead of the error message you may also raise an exception.
            print(
                "Warning: unknown tpe={!r}".format(data["tpe"]),
                file=sys.stderr
            )

The unconditional else allows you to ensure that every line will be handled.



More information about the Tutor mailing list