Input data from .txt file and object array problem

S

SamuelXiao

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

odeits

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
 
P

Piet van Oostrum

SamuelXiao said:
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.
 
S

SamuelXiao

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.

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

odeits

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)

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.

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()
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top