[Tutor] Only appending one object to list, when I am expecting more than 1
AdamC
kabads at gmail.com
Tue Feb 26 04:09:56 EST 2019
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?
Adam
--
You back your data up on the same planet?
http://kabads.monkeez.org <https://kabads.monkeez.org>
PGP key: 0x7111B833
More information about the Tutor
mailing list