Input data from .txt file and object array problem

Piet van Oostrum piet at
Thu Mar 12 16:17:23 CET 2009

>>>>> SamuelXiao <foolsmart2005 at> (S) wrote:

>S> I want to input data by using pickle
>S> First of all, I have a database.txt
>S> The content is like:

>S> AAA,aaalink
>S> BBB,bbblink
>S> CCC,ccclink
>S> ...,...

>S> AAA,BBB,CCC is Language name, and aaalink,bbblink,ccclink is their
>S> respective link.
>S> I want to store data by using pickle.  Meanwhile , I got a problem.
>S> #I have created a class:
>S> class Lang:
>S> 	def __init__(self,lname="",tlink="",alink=""):
>S> 		self.lname = lname #lname is the Language
>S> 		self.tlink = tlink #tlink is the tutorial link
>S> 		self.alink = alink #alink is the a link to school Library finding
>S> the first returned Language book

>S> 	def alibrary_link(self,alink):
>S> 		self.alink = alink

>S> 	def tutorial_link(self,tlink):
>S> 		self.tlink = tlink

>S> 	def lang_name(self,lname):
>S> 		self.lname = lname

>S> 	def _display(self):
>S> 		string = "+++++++++++++++"  + \
>S> 				"+" + lname \
>S> 				"+" + tlink \
>S> 				"+" + alink \
>S> 				"+++++++++++++++++"

This will not do very much. The string is calculated and then thrown
away. You will need also a "return string" to make it useful.

>S> 	def Print(self):
>S> 		print self._display()

>S> def originData():
>S> 	fo = ("/database.txt","r+")
>S> 	lines = fo.readlines()
>S> 	for line in lines:
>S> 		pair = line.split(",")
>S> 		temp = Lang();
>S> 		temp.lname = pair[0]
>S> 		temp.tlink = pair[1]
>S> 		temp.alink = findbook(temp.lname)

Why do you set these attributes directly while you also have methods for
this (like lang_name, tutorial_link)? Or better use 
                temp = Lang(pair[0], pair[1], findbook(temp.lname))

>S> 	#stopping here, because I don't know how to do here...
>S>        #I want to use object array here...
>S>        #Then using pickle to dump the object...
>S>        # Is this work?  Or there is another better method to do so?
>S> I hope to create an object array to store all the language and its
>S> information from database.txt.

I guess you want to put them in a list. Then use 
here and 
objList = [] before the loop.

>S> How can I do that?  I am a beginner to Python.  Any help would be
>S> appreciated.

    You can use pickle to store the list is a file after reading.
    Something like (untested):

import pickle # or use the faster cPickle module.

output = open('somefile', 'wb')

# Pickle object list
pickle.dump(objList, output)


But as this is only textual data (I think) there is not much profit in
using pickle. Unless you have other things added in your class. You
could also write these things to a simple text file, for example with
the csv module.

Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list