String manipulation

Alexander Schmolck a.schmolck at gmail.com
Thu Apr 5 06:50:15 EDT 2007


marco.minerva at gmail.com writes:

> On 4 Apr, 21:47, Alexander Schmolck <a.schmo... at gmail.com> wrote:
> > marco.mine... at gmail.com writes:
> > > Thank you very much, your code works perfectly!
> >
> > One thing I forgot: you might want to make the whitespace handling a bit more
> > robust/general e.g. by using something along the lines of
> >
> >   set_phrase.replace(' ', r'\w+')

Oops, sorry I meant r'\s+'.

> >
> > 'as
> 
> Hi!
> Thanks again... But where must I insert this instruction?

If you're sure the code already does what you want you can forget about my
remark; I was thinking of transforming individual patterns like so: 'kindest
regard' -> r'kindest\w+regard', but it really depends on the details of your
spec, which I'm not familiar with.

For example you clearly want to do some amount of whitespace normalization
(because you use ``.strip()``), but how much? The most extreme you could go is

 input = " ".join(file.read().split()) # all newlines, tabs, multiple spaces -> " "

In which case you don't need to worry about modifying the patterns to take
care of possible whitespace variations. Another possibility is that you
specify the patterns you want to replace as regexps in the file e.g. 

 \bkind(?:est)?\b\s+regard(?:s)?\b
 \byours,\b
 ...

In any case I'd suggest the following: think about what possible edge cases
your input can contain and how you'd like to handle then; then write them up
as unittests (use doctest or unittest and StringIO) and finally modify your
code until it passes all the tests. Here are some examples of possible test
patterns:


- """kindest regard,"""
- """kindest     regard"""
- """kindest\tregard"""
- """kind regards"
- """mankind regards other species as inferior"""
- """... and please send your wife my kindest
  regards,"""

Finally, if you're looking for a programming excercise you could try the
following: rather than working on strings and using regexps, work on a
"stream" of words (i.e. ["kindest", "regards", ...]) and write your own code
to match sequences of words.

'as

p.s. BTW, I overlooked the ``.readlines()`` before, but you don't need it --
files are iterable and you also want to hang on to the openend file object so
that you can close it when you're done.



More information about the Python-list mailing list