[Tutor] Easy Problem

Kent Johnson kent37 at tds.net
Tue Sep 1 16:24:24 CEST 2009


On Mon, Aug 31, 2009 at 9:43 PM, Ishan Puri<ballerz4ishi at sbcglobal.net> wrote:
> Hello,
>     I have 2 plain text documents that have uneven spacing. I need to make
> these single spaced between lines and between words. Basically I need to get
> them to be equal character length after I abridge the uneven spacing.

I don't know what you mean by "equal character length" here.

> In
> Python there is probably one simple command for this for a text file? How do
> I do this?
> E.G.: Hi          how are you?
> Fixed: Hi how are you?

Another way to do this is with regular expressions, for example
In [1]: import re

In [2]: txt = "Hi          how are you?"

In [4]: txt = re.sub(r'  +', ' ', txt)

In [5]: txt
Out[5]: 'Hi how are you?'

You can also replace multiple newlines with single newlines. The
details of that will depend on the line endings in your text; assuming
\n for newline then you could use
  txt = re.sub(r'\n\n+', '\n', txt)
to remove extra newlines.

The split() method won't distinguish between spaces and newlines so
you have to apply it one line at a time. The re.sub() method can work
on the entire text at once.

Kent


More information about the Tutor mailing list