searching files 
Author Message
 searching files

I am trying to make a simple address book program, as an excersise in my
programming skills, and I can make everything except the search function.
I loaded the entire file into a list, and I tried a few things, but it
doesn't work.

Anyone have any ideas, algorithms, code? Thanks!

Nikola



Wed, 27 Oct 2004 22:47:35 GMT  
 searching files

Quote:
> I am trying to make a simple address book program, as an excersise in my
> programming skills, and I can make everything except the search function.
> I loaded the entire file into a list, and I tried a few things, but it
> doesn't work.

generaly you can use a "for" loop.

def find(name):
        for address in addressList:
                if address.find(name) >= 0:
                        return address
        raise Exception("not found")

Quote:
> Anyone have any ideas, algorithms, code? Thanks!

please provide more information about your data structure and what exactly
you want to search (names, numbers etc) and you will get more useful
feedback from this group.

chris

--



Wed, 27 Oct 2004 23:58:01 GMT  
 searching files

Quote:
> please provide more information about your data structure and what exactly
> you want to search (names, numbers etc) and you will get more useful
> feedback from this group.

Well, I am storing every entry in a file. It stores only the name and the
telephone number, and is tab delimited (name surname [TAB] tel.). I am
trying to write a function to which a searchstring would be passed which
would look into the entire file and show the entry that matches.
I would like it to search both names and tel. numbers.

Thank you, once again.



Thu, 28 Oct 2004 02:00:35 GMT  
 searching files

Quote:


>> please provide more information about your data structure and what
>> exactly you want to search (names, numbers etc) and you will get more
>> useful feedback from this group.

> Well, I am storing every entry in a file. It stores only the name and
> the telephone number, and is tab delimited (name surname [TAB] tel.).

in the spirit of ReiserFS (he says you would need a database if your
filesystem would be good enough)... a file per entry... oh well, surely a
possibility but not the simplest. (storing all addresses in the same file,
one line per address, tab delimited entries per line would be simpler)

you could also laod and save pickled python data i.e. save the whole
address list in a file and open it on the next run. that way you don't have
to encode/decode textfiles yourself.

Quote:
> I am trying to write a function to which a searchstring would be
> passed which would look into the entire file and show the entry that
> matches. I would like it to search both names and tel. numbers.

load all addresses in a list then search though the list.

with one line per file:

for filename in os.listdir('databasedir')
        addressList = []
        addressList.append(open(filename).read().split('\t'))

this gives you a list of lists like:
addressList = [
        ['chris', '1234'],
        ['zweistein', '1234'],
]

so you can search:
for name,number in addressList:
        if name == 'chris':
                print name, number
                break #found one, don't search for more
else:
        print "no matching entries found"

as you see there is no errorchecking in the above bits, for a real usabel
programm one should check for illegal entries etc.

a more advanced approch using classes could also be interesting...

chris

--



Thu, 28 Oct 2004 02:24:37 GMT  
 searching files


Quote:
> so you can search:
> for name,number in addressList:
>     if name == 'chris':
>         print name, number
>         break #found one, don't search for more
> else:
>     print "no matching entries found"

> as you see there is no errorchecking in the above bits, for a real usabel
> programm one should check for illegal entries etc.

Thank you, I'll try this!


Thu, 28 Oct 2004 02:42:28 GMT  
 searching files

Quote:



>> please provide more information about your data structure and what exactly
>> you want to search (names, numbers etc) and you will get more useful
>> feedback from this group.

> Well, I am storing every entry in a file. It stores only the name and the
> telephone number, and is tab delimited (name surname [TAB] tel.). I am
> trying to write a function to which a searchstring would be passed which
> would look into the entire file and show the entry that matches.
> I would like it to search both names and tel. numbers.

This is no-brainer in Unix, ie.
    egrep 'surname' file
    awk '/surname/ {print}' file
In pure Python, you have to read the file into list, and do string/re search.

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin



Thu, 28 Oct 2004 03:21:04 GMT  
 searching files
Hmmmmmm... Yes, I know this, but I'm currently on Win2k, and do not have
access to grep and/or awk.

Nikola


Quote:



> >> please provide more information about your data structure and what
exactly
> >> you want to search (names, numbers etc) and you will get more useful
> >> feedback from this group.

> > Well, I am storing every entry in a file. It stores only the name and
the
> > telephone number, and is tab delimited (name surname [TAB] tel.). I am
> > trying to write a function to which a searchstring would be passed which
> > would look into the entire file and show the entry that matches.
> > I would like it to search both names and tel. numbers.

> This is no-brainer in Unix, ie.
>     egrep 'surname' file
>     awk '/surname/ {print}' file
> In pure Python, you have to read the file into list, and do string/re
search.

> --

> 8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin



Thu, 28 Oct 2004 18:52:11 GMT  
 searching files

Quote:

> Hmmmmmm... Yes, I know this, but I'm currently on Win2k, and do not have
> access to grep and/or awk.

If you need grep and/or awk just to get a job done, try this:

http://gnuwin32.sourceforge.net/

and you can get a Windows executable of the one true awk from the
k-man himself:

http://plan9.bell-labs.com/cm/cs/awkbook/index.html

OTOH, writing awk scripts that are too long to fit on a screen will
fry your brain. Learning to write such things as simple greppers in
Python will expand your horizons and bring much less sorrow to anyone
(including yourself) who has to read the code later.

HTH,
John



Fri, 29 Oct 2004 06:28:14 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. number of records for tps file and searching files

2. search files

3. Search file and print line + next line?

4. Search file procedure

5. FYI - Re: Speed of searching file using Queues

6. search file dbfntxax.ch

7. searching files

8. TSO Rexx to search file for string

9. searching files into a table

10. search files / directory listing

11. Search file !!!

12. Searching files

 

 
Powered by phpBB® Forum Software