Input data from .txt file and object array problem

Discussion in 'Python' started by SamuelXiao, Mar 12, 2009.

  1. SamuelXiao

    SamuelXiao Guest

    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.
    SamuelXiao, Mar 12, 2009
    #1
    1. Advertising

  2. SamuelXiao

    odeits Guest

    On Mar 12, 5:03 am, SamuelXiao <> 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
    odeits, Mar 12, 2009
    #2
    1. Advertising

  3. >>>>> SamuelXiao <> (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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Mar 12, 2009
    #3
  4. SamuelXiao

    SamuelXiao Guest

    On Mar 12, 11:17 pm, Piet van Oostrum <> wrote:
    > >>>>> SamuelXiao <> (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 <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email:


    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.
    SamuelXiao, Mar 12, 2009
    #4
  5. SamuelXiao

    odeits Guest

    On Mar 12, 9:02 am, SamuelXiao <> wrote:
    > On Mar 12, 11:17 pm, Piet van Oostrum <> wrote:
    >
    >
    >
    > > >>>>> SamuelXiao <> (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 <>
    > > URL:http://pietvanoostrum.com[PGP8DAE142BE17999C4]
    > > Private email:

    >
    > 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.


    datafile = open('datafile.txt','rb')
    pairs = [line.strip().split(',',1) for line in datafile]
    datafile.close()
    textfile = open('essay.txt','rb')
    text = textfile.read()
    textfile.close()
    for word,link in pairs:
    text = text.replace(word, link) # change this to decorate the link
    with the <a href stuff and what not
    textfile = open('essay.txt', 'wb')
    textfile.write(text)
    textfile.close()
    odeits, Mar 13, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyberco
    Replies:
    6
    Views:
    676
    John Machin
    Nov 20, 2006
  2. Sameen
    Replies:
    2
    Views:
    421
    Victor Bazarov
    Aug 29, 2005
  3. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,463
    Roedy Green
    Sep 15, 2011
  4. Cool Wong
    Replies:
    3
    Views:
    106
    James Edward Gray II
    Jun 20, 2007
  5. Ram
    Replies:
    3
    Views:
    241
    Tad McClellan
    Apr 26, 2007
Loading...

Share This Page