[Tutor] newbie search and replace

Michael Janssen Janssen@rz.uni-frankfurt.de
Mon Jan 27 20:08:13 2003


Hi all,


to avoid nasty regular expressions, one can split the file into "words"
(defined as character[s] between spaces - now you needn't check for
word-boundaries via regexp) and check for every word if a
abbrevation-fullstring-dictionary has the word as a key:

1. read file with readlines (--> list of lines)

2. split every line into "words". Omit newlines

3. check for each word in lowercase if the dictionary has it as a key. If
yes change it (i.e. overwrite the word with the fullstring from the
dictionary).

4. join the words back to lines.

5. write list of lines back to file.


This approach has the advantages of avoiding regexps, possibly being
faster (could be, but I don't know), being more readable, because the
abbrevation-fullstring definitions sits in the dictionary and arn't
scattered through the code.

On the other hand, the code itself is more comlicate than simple read(),
xxx times of re.sub() and write(). Perhaps, you have solved your task just
a few minutes after first response from the list, how knows?

regards

Michael

On Tue, 28 Jan 2003, Magnus Lycka wrote:


> But to use re well, you need some study and practice. See
> the python library reference.