Input data from .txt file and object array problem

SamuelXiao foolsmart2005 at gmail.com
Thu Mar 12 12:02:03 EDT 2009


On Mar 12, 11:17 pm, Piet van Oostrum <p... at cs.uu.nl> wrote:
> >>>>> SamuelXiao <foolsmart2... at gmail.com> (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
>                 objList.append(temp)
> 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)
>
> output.close()
>
> 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 <p... at cs.uu.nl>
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p... at vanoostrum.org

Hi, odeits, Piet van Oostrum, thanks for your reply.  But I found that
my school's server have problem with reading csv file.  So, I think I
have to do it another way.  By the way, is there any better way to
store data?  Actually, what I want to do is like:

1. Given a post or an essay, I want to find any language names in the
post or essay matching with data in my database.txt
2. Then insert <a href="tutoriallink" title="school library book
title"></a> about the matched word.

My original version is super slow because each time go to and back
from the library website wastes a lot of times.  I hope to use class
to store the data in database.txt first, then store the book
information from library.  Then visited matched word no need to go to
search again.  Just simply insert <a...></a> about it.  But my problem
is how to store object array and call it out?  Thanks for any help.



More information about the Python-list mailing list