Stripping whitespace

Reedick, Andrew jr9445 at ATT.COM
Wed Jan 23 15:57:10 EST 2008


> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of ryan k
> Sent: Wednesday, January 23, 2008 3:24 PM
> To: python-list at python.org
> Subject: Re: Stripping whitespace
> 
> On Jan 23, 3:02 pm, John Machin <sjmac... at lexicon.net> wrote:
> > On Jan 24, 6:57 am, ryan k <r... at ryankaskel.com> wrote:
> >
> > > So yea i will just have to count dashes.
> >
> > Read my lips: *you* counting dashes is dumb. Writing your code so
> that
> > *code* is counting dashes each time it opens the file is smart.
> 
> Okay it's almost working ...
> 

Why is it that so many Python people are regex adverse?  Use the dashed
line as a regex.  Convert the dashes to dots.  Wrap the dots in
parentheses.  Convert the whitespace chars to '\s'.  Presto!  Simpler,
cleaner code.

import re

state = 0
header_line = ''
pattern = ''
f = open('a.txt', 'r')
for line in f:
	if line[-1:] == '\n':
		line = line[:-1]

	if state == 0:
		header_line = line
		state += 1
	elif state == 1:
		pattern = re.sub(r'-', r'.', line)
		pattern = re.sub(r'\s', r'\\s', pattern)
		pattern = re.sub(r'([.]+)', r'(\1)', pattern)
		print pattern
		state += 1

		headers = re.match(pattern, header_line)
		if headers:
			print headers.groups()
	else:
		state = 2
		m = re.match(pattern, line)
		if m:
			print m.groups()


f.close()



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625





More information about the Python-list mailing list