Python Forum

Full Version: Saving and loading many records
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys

I am currently trying to get my python program to save and load from one text file

Basically i am creating a student record program which writes and loads within the programme, I am using pickle at the moment. I have successfully got each record to save in a individual text file.

However I am unsure of number 6,

Could anyone direct me to an example I can see for question 5 and 6 please,

5. retrieve and display the details of any student when Mr Lee enters the student’s unique ID
number.*

6 create at least three different reports that Mr Lee might need

import pickle
import os.path

def savedata():
    """
    This method add new entry.
    @take input as a name from console.
    @take input as a phone from console..
    @ save name and phone in file
    """

    studentld = input("Please enter your name:  ")
    firstname = input("Enter your Mobile number : ")
    lastname = input("Enter your Mobile Network  : ")
    outFile = open(studentld + '.txt', 'wb')

    pickle.dump({ "studentld": studentld, "firstname": firstname, "network": lastname}, outFile)
    
    outFile.close()

    print("Successfully added a new name and number & network for:"+ studentld+ "\n")



def get_entry():
    """
    This method print number already added.
    @take input as a name from console.
    @find antry of input name and print mobile number 
    """
    saveln = input("Please enter your name:")
    try:
        open_file = open(saveln + '.txt', 'rb')
        load_file = pickle.load(open_file)
        
        print("Your name is: "+load_file['studentld']+"\n")
        print("Your telephone number: "+load_file['firstname']+"\n")
        print("Your telephone network: "+load_file['lastname']+"\n")
        open_file.close()
    except:
        print("\n File does not exist!!")
I never use pickle,json is a better choice.
But for this a database is better.
dataset is easy,i have a tutorial here
So could look like this for your task.
>>> import dataset
>>> db = dataset.connect('sqlite:///cartoon.db')
>>> table = db['cartoon']
>>> r1 = { "studentld": 50, "firstname": 'Homer', "lastname": 'Simpson'}
>>> r2 = { "studentld": 100, "firstname": 'Clark', "lastname": 'Superman'}
>>> table.insert(r1)
1
>>> table.insert(r2)
2

# look at db
>>> [user for user in db['cartoon']]
[OrderedDict([('id', 1), ('studentld', 50), ('lastname', u'Simpson'), ('firstname', u'Homer')]),
OrderedDict([('id', 2), ('studentld', 100), ('lastname', u'Superman'), ('firstname', u'Clark')])]

# Find one
>>> homer = john = table.find_one(firstname='Homer')
>>> homer
OrderedDict([('id', 1), ('studentld', 50), ('lastname', u'Simpson'), ('firstname', u'Homer')])

# SQL queries
>>> [i for i in db.query("SELECT MAX(studentld) FROM cartoon")]
[OrderedDict([(u'MAX(studentld)', 100)])]
Freeze to json.
result = db['cartoon'].all()
dataset.freeze(result, format='json', filename='cartoon.json')

# Read json
>>> import json
>>> with open('cartoon.json') as j:
...     result = json.load(j)
...     
>>> result
{u'count': 2,
u'meta': {},
u'result': [{u'firstname': u'Homer',
               u'id': 1,
               u'lastname': u'Simpson',
               u'studentld': 50},
              {u'firstname': u'Clark',
               u'id': 2,
               u'lastname': u'Superman',
               u'studentld': 100}]}
Sounds like 'Homework' would have been a better location for this post.
Hi there Snipppsat.

I think getting it to save and read from the database would be complex for me at this stage. Is there text file method, I have learnt pickle so far but cannot implement what the question is asking me to.