[Tutor] textfile or database - has appeared as: Looking for some utilities

Michael Janssen Janssen@rz.uni-frankfurt.de
Fri Jul 25 14:43:02 2003


[the former subject was from another tread - a bit confusing]

On Fri, 25 Jul 2003, Emily Lea Connally wrote:

> Hey everyone, I'm very new to programming, in fact, Python is my first

Hello Emily.

> language. I've been teaching myself for the past week or so, and I ran
> into a problem with the program I'm currently trying to write.
> I need to read the file and search for an address that the user had
> entered earlier in the program and delete that address if found. Can I do
> this using a text file or should I set up a database or what?

Both are reasonable choises. Databases are faster and has already commands
to search, delete, update entries. Textfiles are simpler to use (you can
e.g. watch and edit them with an editor). Using a textfile ist perhaps
the best choice for a small project.

One important aspect is the organization of the data. Let me explain:
Suppose you have got no data organisation at all, say a file with random
content and something between all kind of stuff is an address. A textfile
is perfekt for this scenario, cause you just have to "search & replace"
the address (replace by nothing to delete it). string.replace(address, '',
file_as_string) would do it.

Suppose you organise your data a bit more and put each single address on a
line by itself (think of a list of email addresses), you can very well use
a textfile, read it in line oriented manner (file_object.readline()), find
the line containing the address and delete it.

Next step. An address can consist of several informations (like phone
number, snail mail address and so on). One way to handle this is to use
identifiers on each line:

"""
phone_number:12345
country:de
....
"""

There is no point in reading such a file line by line any longer. You need
to read it paragraph for paragraph - and you must write your own function/
programm logic for this job. Also deleting an address is now something
slightly different. But anyway, a textfile can hold such informations and
its not that hard to write a script to manipulate these informations.

But when you want to do more and more tricky things like how often was an
address actually used or if you want to combine one type of information
(say addresses) with other types (say payments or bills) you might better
off using a full blown database.


Back to your post: it might well be that you can accomplish your task with
a textfile but that's hard to tell, cause we don't know what your data is
like. In case you havn't got a clue yet, you should post how your textfile
would be organised and which code you have written that far to manipulate
it.


> Sorry if this question is a little elementary,

Well, I find it the strength of tutor, to cope with elementary questions.
I've seen mailing lists that try but were unable to explain simple things
but python-tutor was often able to do so. Who knows? Perhaps some time
somebody exposes a question to tutor, anybody *knows* the answere but -
there's ain't nobody out there who can explain it ;-)

With other words: tutor is the right place to ask simple questions.

Michael


> but I am honestly brand new
> to programming and really stuck here. The tutorials I've read online are
> helpful, but I still can't seem to get it right.
> thanks for any help you can offer
> M
>
> *************************************************************************
>     Forgiveness is the fragrance the violet sheds on the heel that has
>   crushed it.
> 	- Mark Twain
> *************************************************************************
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>