Input data from .txt file and object array problem
odeits
odeits at gmail.com
Thu Mar 12 08:14:57 EDT 2009
On Mar 12, 5:03 am, SamuelXiao <foolsmart2... at gmail.com> wrote:
> I want to input data by using pickle
> First of all, I have a database.txt
> The content is like:
>
> AAA,aaalink
> BBB,bbblink
> CCC,ccclink
> ...,...
>
> AAA,BBB,CCC is Language name, and aaalink,bbblink,ccclink is their
> respective link.
> I want to store data by using pickle. Meanwhile , I got a problem.
> #I have created a class:
> class Lang:
> def __init__(self,lname="",tlink="",alink=""):
> self.lname = lname #lname is the Language
> self.tlink = tlink #tlink is the tutorial link
> self.alink = alink #alink is the a link to school Library finding
> the first returned Language book
>
> def alibrary_link(self,alink):
> self.alink = alink
>
> def tutorial_link(self,tlink):
> self.tlink = tlink
>
> def lang_name(self,lname):
> self.lname = lname
>
> def _display(self):
> string = "+++++++++++++++" + \
> "+" + lname \
> "+" + tlink \
> "+" + alink \
> "+++++++++++++++++"
>
> def Print(self):
> print self._display()
>
> def originData():
> fo = ("/database.txt","r+")
> lines = fo.readlines()
> for line in lines:
> pair = line.split(",")
> temp = Lang();
> temp.lname = pair[0]
> temp.tlink = pair[1]
> temp.alink = findbook(temp.lname)
> #stopping here, because I don't know how to do here...
> #I want to use object array here...
> #Then using pickle to dump the object...
> # Is this work? Or there is another better method to do so?
>
> I hope to create an object array to store all the language and its
> information from database.txt.
> How can I do that? I am a beginner to Python. Any help would be
> appreciated.
check out the csv module for the parsing of the file
http://docs.python.org/library/csv.html
More information about the Python-list
mailing list