[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Stripping whitespace

ryan@ryankaskel.com

1/23/2008 6:50:00 PM

Hello. I have a string like 'LNAME
PASTA ZONE'. I want to create a list of those words and
basically replace all the whitespace between them with one space so i
could just do lala.split(). Thank you!

Ryan Kaskel
21 Answers

Marc 'BlackJack' Rintsch

1/23/2008 7:05:00 PM

0

On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:

> Hello. I have a string like 'LNAME
> PASTA ZONE'. I want to create a list of those words and
> basically replace all the whitespace between them with one space so i
> could just do lala.split(). Thank you!

You *can* just do ``lala.split()``:

In [97]: lala = 'LNAME PASTA ZONE'

In [98]: lala.split()
Out[98]: ['LNAME', 'PASTA', 'ZONE']

Ciao,
Marc 'BlackJack' Rintsch

Paul Rubin

1/23/2008 7:05:00 PM

0

ryan k <ryan@ryankaskel.com> writes:
> Hello. I have a string like 'LNAME
> PASTA ZONE'. I want to create a list of those words and
> basically replace all the whitespace between them with one space so i
> could just do lala.split(). Thank you!

import re
s = 'LNAME PASTA ZONE'
re.split('\s+', s)

John Machin

1/23/2008 7:10:00 PM

0

On Jan 24, 5:50 am, ryan k <r...@ryankaskel.com> wrote:
> Hello. I have a string like 'LNAME
> PASTA ZONE'. I want to create a list of those words and
> basically replace all the whitespace between them with one space so i
> could just do lala.split(). Thank you!
>
> Ryan Kaskel

So when you go to the Python interactive prompt and type firstly
lala = 'LNAME PASTA ZONE'
and then
lala.split()
what do you see, and what more do you need to meet your requirements?

James Matthews

1/23/2008 7:11:00 PM

0

Using the split method is the easiest!

On 23 Jan 2008 19:04:38 GMT, Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
> On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:
>
> > Hello. I have a string like 'LNAME
> > PASTA ZONE'. I want to create a list of those words and
> > basically replace all the whitespace between them with one space so i
> > could just do lala.split(). Thank you!
>
> You *can* just do ``lala.split()``:
>
> In [97]: lala = 'LNAME PASTA ZONE'
>
> In [98]: lala.split()
> Out[98]: ['LNAME', 'PASTA', 'ZONE']
>
> Ciao,
> Marc 'BlackJack' Rintsch
>
> --
> http://mail.python.org/mailman/listinfo/p...
>



--
http://search.goldwatches.com/?Search=Mova...
http://www.jewelers...
http://www.goldw...

ryan@ryankaskel.com

1/23/2008 7:13:00 PM

0

On Jan 23, 2:04 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Wed, 23 Jan 2008 10:50:02 -0800, ryan k wrote:
> > Hello. I have a string like 'LNAME
> > PASTA ZONE'. I want to create a list of those words and
> > basically replace all the whitespace between them with one space so i
> > could just do lala.split(). Thank you!
>
> You *can* just do ``lala.split()``:

Indeed you can thanks!

>
> In [97]: lala = 'LNAME PASTA ZONE'
>
> In [98]: lala.split()
> Out[98]: ['LNAME', 'PASTA', 'ZONE']
>
> Ciao,
> Marc 'BlackJack' Rintsch

ryan@ryankaskel.com

1/23/2008 7:18:00 PM

0

I am taking a database class so I'm not asking for specific answers.
Well I have this text tile:

http://www.cs.tufts.edu/comp/115/projects/proj0/cu...

And this code:

# Table and row classes used for queries

class Row(object):
def __init__(self, column_list, row_vals):
print len(column_list)
print len(row_vals)
for column, value in column_list, row_vals:
if column and value:
setattr(self, column.lower(), value)

class Table(object):
def __init__(self, table_name, table_fd):
self.name = table_name
self.table_fd = table_fd
self.rows = []
self._load_table()

def _load_table(self):
counter = 0
for line in self.table_fd:
# Skip the second line
if not '-----' in line:
if counter == 0:
# This line contains the columns, parse it
column_list = line.split()
else:
# This is a row, parse it
row_vals = line.split()
# Create a new Row object and add it to the
table's
# row list
self.rows.append(Row(column_list, row_vals))
counter += 1

Because the addresses contain spaces, this won't work because there
are too many values being unpacked in row's __init__'s for loop. Any
suggestions for a better way to parse this file? I don't want to cheat
but just some general ideas would be nice. Thanks!

John Machin

1/23/2008 7:21:00 PM

0

On Jan 24, 6:05 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> ryan k <r...@ryankaskel.com> writes:
> > Hello. I have a string like 'LNAME
> > PASTA ZONE'. I want to create a list of those words and
> > basically replace all the whitespace between them with one space so i
> > could just do lala.split(). Thank you!
>
> import re
> s = 'LNAME PASTA ZONE'
> re.split('\s+', s)

That is (a) excessive for the OP's problem as stated and (b) unlike
str.split will cause him to cut you out of his will if his problem
turns out to include leading/trailing whitespace:

>>> lala = ' LNAME PASTA ZONE '
>>> import re
>>> re.split(r'\s+', lala)
['', 'LNAME', 'PASTA', 'ZONE', '']
>>> lala.split()
['LNAME', 'PASTA', 'ZONE']
>>>

John Machin

1/23/2008 7:53:00 PM

0

On Jan 24, 6:17 am, ryan k <r...@ryankaskel.com> wrote:
> I am taking a database class so I'm not asking for specific answers.
> Well I have this text tile:
>
> http://www.cs.tufts.edu/comp/115/projects/proj0/cu...

Uh-huh, "column-aligned" output.

>
> And this code:
>
[snip]

>
> Because the addresses contain spaces, this won't work because there
> are too many values being unpacked in row's __init__'s for loop. Any
> suggestions for a better way to parse this file?


Tedious (and dumb) way:
field0 = line[start0:end0+1].rstrip()
field1 = line[start1:end1+1].rstrip()
etc

Why dumb: if the column sizes change, you have to suffer the tedium
again. While your sample appears to pad out each field to some
predetermined width, some reporting software (e.g. the Query Analyzer
that comes with MS SQL Server) will tailor the widths to the maximum
size actually observed in the data in each run of the report ... so
you write your program based on some tiny test output and next day you
run it for real and there's a customer whose name is Marmaduke
Rubberduckovitch-Featherstonehaugh or somesuch and your name is mud.

Smart way: note that the second line (the one with all the dashes)
gives you all the information you need to build lists of start and end
positions.

ryan@ryankaskel.com

1/23/2008 7:57:00 PM

0

On Jan 23, 2:53 pm, John Machin <sjmac...@lexicon.net> wrote:
> On Jan 24, 6:17 am, ryan k <r...@ryankaskel.com> wrote:
>
> > I am taking a database class so I'm not asking for specific answers.
> > Well I have this text tile:
>
> >http://www.cs.tufts.edu/comp/115/projects/proj0/cu...
>
> Uh-huh, "column-aligned" output.
>
>
>
> > And this code:
>
> [snip]
>
>
>
> > Because the addresses contain spaces, this won't work because there
> > are too many values being unpacked in row's __init__'s for loop. Any
> > suggestions for a better way to parse this file?
>
> Tedious (and dumb) way:
> field0 = line[start0:end0+1].rstrip()
> field1 = line[start1:end1+1].rstrip()
> etc
>
> Why dumb: if the column sizes change, you have to suffer the tedium
> again. While your sample appears to pad out each field to some
> predetermined width, some reporting software (e.g. the Query Analyzer
> that comes with MS SQL Server) will tailor the widths to the maximum
> size actually observed in the data in each run of the report ... so
> you write your program based on some tiny test output and next day you
> run it for real and there's a customer whose name is Marmaduke
> Rubberduckovitch-Featherstonehaugh or somesuch and your name is mud.
>
> Smart way: note that the second line (the one with all the dashes)
> gives you all the information you need to build lists of start and end
> positions.

Thank you for your detailed response Mr. Machin. The teacher *said*
that the columns were supposed to be tab delimited but they aren't. So
yea i will just have to count dashes. Thank you!

John Machin

1/23/2008 8:03:00 PM

0

On Jan 24, 6:57 am, ryan k <r...@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.