-1

Suppose I have a file named abc.txt which contains the following data

Nathan  Johnson 23 M
Mary    Kom     28 F
John    Keyman  32 M
Edward  Stella  35 M

How do i create the objects of the data(records) in the file ?

Code which i have done . I am not getting of jow to create objects of data in a file

class Records:
    def __init__(self, firstname, lastname, age, gender):
        self.fname = firstname        
        self.lname = lastname 
        self.age = age 
        self.gender = gender
    def read(self):
        f= open("abc.txt","r")
        for lines in f:
            print lines.split("\t") 

What should i do further ? I am an newbie in python and this task has been given to me. PLease help me out ?

John john
  • 25
  • 4
  • 1
    You would have to read the contents of the file line by line, and split each line by (what looks like) the tab character (`\t`). The resulting list would contain the name at the first and second index, the age at the third index and the gender at the last index. – Lix Oct 21 '13 at 06:42
  • 4
    You might want to read http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files – thefourtheye Oct 21 '13 at 06:42
  • i have updated the code? now how do i create objects of data in a file ?? Although i can create my own objects with attributes but i want to create objects of the data (records) in an file – John john Oct 21 '13 at 06:47

1 Answers1

3

Although you've used an object here, namedtuple would have been more suitable.

# Creating the class
class Records:
    def __init__(self, firstname, lastname, age, gender):
        self.fname = firstname
        self.lname = lastname
        self.age = age
        self.gender = gender

# Using open to open the required file, and calling the file f,
# using with automatically closes the file stream, so its less
# of a hassle.
with open('object_file.txt') as f:
    list_of_records = [Records(*line.split()) for line in f]  # Adding records to a list

for record in list_of_records:
    print record.age  # Printing a sample
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • Records are already there in the file, i just need to make there objects and depending upon the firstname, lastname, age or gender, i will make a search – John john Oct 21 '13 at 06:55
  • what does this * do ?? – John john Oct 21 '13 at 07:04
  • @Johnjohn Its called the splat [operator](http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists). – Games Brainiac Oct 21 '13 at 07:10
  • what does it do here beside from splitting ? – John john Oct 21 '13 at 07:11
  • @Johnjohn Please read the documentation and the code samples, under "Unpacking Argument Lists". It will explain everything. – Games Brainiac Oct 21 '13 at 07:13
  • @Johnjohn This might be helpful [too](http://hangar.runway7.net/python/packing-unpacking-arguments). Get in the habbit of reading documentation and articles about things you don't know. – Games Brainiac Oct 21 '13 at 07:16
  • ok..so here *line.split() packs all the records of line from a file and store it in a variable ...... – John john Oct 21 '13 at 07:24
  • @Johnjohn Well `line.split()` splits the line by spaces. `*`, then unpacks the different variables that are now contained within the list, and makes them parameters to your `Record`'s constructor. – Games Brainiac Oct 21 '13 at 07:26
  • is the list_of_records are my object to Record class ?? or its a list?? To what I understand I consider it as a list which holds the records of an file – John john Oct 21 '13 at 07:31
  • @Johnjohn Its called `list_of_records`. Its a list. Its a list that contains your record objects. I've written the code, so that all the records in a particular file, are made into Records, and then added to a list. – Games Brainiac Oct 21 '13 at 07:33
  • give me sometime to understand. If i'll have aquery i will add the comment. I am jus a newbie so I might ask some silly question. Thank you in advance :) – John john Oct 21 '13 at 07:39
  • @Johnjohn Well _try_ reading some documentation. I know you are a newbie, thats why I'm being patient with you. But say [`split()`](http://stackoverflow.com/questions/4978787/how-to-split-a-string-into-array-of-characters-with-python/4978792#4978792) and how it works, already has an answer here on SO. – Games Brainiac Oct 21 '13 at 07:41
  • Ok. so each record in an file is my object and those objects are being getting into the list one by one. isn't it ? – John john Oct 21 '13 at 07:43
  • @Johnjohn Absolutely right. – Games Brainiac Oct 21 '13 at 07:47
  • I have some more questions to come. Give me 30 mins i will read it first and then ask it..? Again thank you in advance :) – John john Oct 21 '13 at 07:50
  • Now do this fname, lname, age, gender have an index associated with them ?? Because my requirement is that if the user select t search criteria through firstname it should search in firstname column only and display the whole record. – John john Oct 21 '13 at 09:06
  • @Johnjohn Then that is a separate question all together. We should not be making the comments any longer than they already are :P Ask a separate question. – Games Brainiac Oct 21 '13 at 09:09
  • Ok...and thank you for the concepts above. I am raising an seperate question. Plesae do reply on that – John john Oct 21 '13 at 09:14
  • @Johnjohn Sure, happy to. – Games Brainiac Oct 21 '13 at 09:16
  • @ Games Brainiac : http://stackoverflow.com/questions/19490587 – John john Oct 21 '13 at 09:22