Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Stripping whitespace

Reply
Thread Tools

Stripping whitespace

 
 
ryan k
Guest
Posts: n/a
 
      01-23-2008
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
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-23-2008
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      01-23-2008
ryan k <(E-Mail Removed)> 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)
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-23-2008
On Jan 24, 5:50 am, ryan k <(E-Mail Removed)> 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?
 
Reply With Quote
 
James Matthews
Guest
Posts: n/a
 
      01-23-2008
Using the split method is the easiest!

On 23 Jan 2008 19:04:38 GMT, Marc 'BlackJack' Rintsch <(E-Mail Removed)> 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/python-list
>




--
http://search.goldwatches.com/?Search=Movado+Watches
http://www.jewelerslounge.com
http://www.goldwatches.com
 
Reply With Quote
 
ryan k
Guest
Posts: n/a
 
      01-23-2008
On Jan 23, 2:04 pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> 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


 
Reply With Quote
 
ryan k
Guest
Posts: n/a
 
      01-23-2008
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/pro...0/customer.txt

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!
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-23-2008
On Jan 24, 6:05 am, Paul Rubin <http://(E-Mail Removed)> wrote:
> ryan k <(E-Mail Removed)> 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']
>>>

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-23-2008
On Jan 24, 6:17 am, ryan k <(E-Mail Removed)> 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/pro...0/customer.txt


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.
 
Reply With Quote
 
ryan k
Guest
Posts: n/a
 
      01-23-2008
On Jan 23, 2:53 pm, John Machin <(E-Mail Removed)> wrote:
> On Jan 24, 6:17 am, ryan k <(E-Mail Removed)> 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/pro...0/customer.txt

>
> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Eclipse: Stripping whitespace at EOL Tim Tyler Java 7 09-15-2011 04:07 PM
Problem with stripping leading whitespace in here documents Douglas Wells Ruby 8 01-27-2007 07:46 AM
Strip is not stripping trailing whitespace Taylor Strait Ruby 13 12-28-2006 05:04 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM
Stripping whitespace around certain elements Andy Jefferies XML 1 06-26-2003 11:29 AM



Advertisments