473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Stripping whitespace

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
Jan 23 '08 #1
14 2859
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
Jan 23 '08 #2
ryan k <ry**@ryankaskel.comwrites:
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)
Jan 23 '08 #3
On Jan 24, 5:50 am, ryan k <r...@ryankaskel.comwrote:
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?
Jan 23 '08 #4
Using the split method is the easiest!

On 23 Jan 2008 19:04:38 GMT, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
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
Jan 23 '08 #5
On Jan 23, 2:04 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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
Jan 23 '08 #6
On Jan 24, 6:05 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
ryan k <r...@ryankaskel.comwrites:
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']
>>>
Jan 23 '08 #7
On Jan 24, 6:57 am, ryan k <r...@ryankaskel.comwrote:
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.

Jan 23 '08 #8
On Jan 24, 7:23 am, ryan k <r...@ryankaskel.comwrote:
On Jan 23, 3:02 pm, John Machin <sjmac...@lexicon.netwrote:
On Jan 24, 6:57 am, ryan k <r...@ryankaskel.comwrote:
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 ...

new parser function:

def _load_table(self):
counter = 0
for line in self.table_fd:
# Skip the second line
The above comment is a nonsense.
if counter == 0:
# This line contains the columns, parse it
column_list = line.split()
In generality, you would have to allow for the headings to contain
spaces as well -- this means *saving* a reference to the heading line
and splitting it *after* you've processed the line with the dashes.
elif counter == 1:
# These are the dashes
line_l = line.split()
column_width = [len(i) for i in line_l]
Whoops.
column_width = [len(i) + 1 for i in line_l]
print column_width
else:
# This is a row, parse it
marker = 0
row_vals = []
for col in column_width:
start = sum(column_width[:marker])
finish = sum(column_width[:marker+1])
print line[start:finish].strip()
If you had printed just line[start:finish], it would have been obvious
what the problem was. See below for an even better suggestion.
row_vals.append(line[start:finish].strip())
marker += 1
Using sum is a tad ugly. Here's an alternative:

row_vals = []
start = 0
for width in column_width:
finish = start + width
#DEBUG# print repr(line[start:finish].replace(' ', '~'))
row_vals.append(line[start:finish].strip())
start = finish
self.rows.append(Row(column_list, row_vals))
counter += 1

Something obvious you can see wrong with my start finish code?
See above.

Jan 23 '08 #9
On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
ryan k <ry**@ryankaskel.comwrites:
>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)
Please tell me you're making fun of the poor newbie and didn't mean to
seriously suggest using a regex merely to split on whitespace?
>>import timeit
timeit.Timer("s.split()", "s = 'one two three four'").repeat()
[1.4074358940124512, 1.3505148887634277, 1.3469438552856445]
>>timeit.Timer("re.split('\s+', s)", "import re;s = 'one two
three four'").repeat()
[7.9205508232116699, 7.8833441734313965, 7.9301259517669678]

--
Steven
Jan 23 '08 #10
On Jan 23, 5:37 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
ryan k <r...@ryankaskel.comwrites:
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)

Please tell me you're making fun of the poor newbie and didn't mean to
seriously suggest using a regex merely to split on whitespace?
>import timeit
timeit.Timer("s.split()", "s = 'one two three four'").repeat()

[1.4074358940124512, 1.3505148887634277, 1.3469438552856445]>>timeit.Timer("re.split('\s+', s)", "import re;s = 'one two

three four'").repeat()
[7.9205508232116699, 7.8833441734313965, 7.9301259517669678]

--
Steven
The main topic is not an issue anymore.
Jan 23 '08 #11
On Jan 23, 5:37 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Wed, 23 Jan 2008 11:05:01 -0800, Paul Rubin wrote:
ryan k <r...@ryankaskel.comwrites:
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)

Please tell me you're making fun of the poor newbie and didn't mean to
seriously suggest using a regex merely to split on whitespace?
>import timeit
timeit.Timer("s.split()", "s = 'one two three four'").repeat()

[1.4074358940124512, 1.3505148887634277, 1.3469438552856445]>>timeit.Timer("re.split('\s+', s)", "import re;s = 'one two

three four'").repeat()
[7.9205508232116699, 7.8833441734313965, 7.9301259517669678]

--
Steven
Much thanks to Machin for helping with the parsing job. Steven
D'Aprano, you are a prick.
Jan 23 '08 #12
On Jan 24, 9:50 am, ryan k <r...@ryankaskel.comwrote:
Steven D'Aprano, you are a prick.
And your reasons for coming to that stridently expressed conclusion
after reading a posting that was *not* addressed to you are .....?

Jan 23 '08 #13
On Jan 23, 6:30 pm, John Machin <sjmac...@lexicon.netwrote:
On Jan 24, 9:50 am, ryan k <r...@ryankaskel.comwrote:
Steven D'Aprano, you are a prick.

And your reasons for coming to that stridently expressed conclusion
after reading a posting that was *not* addressed to you are .....?
Because his tone is extremely condescending and quite frankly
annoying. From this post to others, his terse remarks scar this
community and fade its atmosphere of friendliness.
Jan 24 '08 #14
On Jan 24, 8:21 am, ryan k <r...@ryankaskel.comwrote:
On Jan 23, 6:30 pm, John Machin <sjmac...@lexicon.netwrote:
On Jan 24, 9:50 am, ryan k <r...@ryankaskel.comwrote:
Steven D'Aprano, you are a prick.
And your reasons for coming to that stridently expressed conclusion
after reading a posting that was *not* addressed to you are .....?

Because his tone is extremely condescending and quite frankly
annoying. From this post to others, his terse remarks scar this
community and fade its atmosphere of friendliness.
And your response was any better because...?
Jan 24 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: joram gemma | last post by:
Hello, on windows python 2.4.1 I have the following problem >>> s = 'D:\\music\\D\\Daniel Lanois\\For the beauty of Wynona' >>> print s D:\music\D\Daniel Lanois\For the beauty of Wynona >>>...
1
by: Andy Jefferies | last post by:
I'm having problems stripping out the whitespace at the beginning of a particular element. In the XML snippet I've highlighted tabs and returns as ^I and ^M respectively: <para> ^I ^I ...
2
by: Wolfgang Jeltsch | last post by:
Hello, it is often convenient to insert whitespace into an XML document in order to format it nicely. For example, take this snippet of a notional DocBook XML document: <para> This is a...
15
by: Jeff North | last post by:
Hi, I'm using a control called HTMLArea which allows a person to enter text and converts the format instructions to html tags. Most of my users know nothing about html so this is perfect for my...
1
by: dingbat | last post by:
Anyone have a recommendation for a tool (Perl script would be ideal) to do the following: - Take a CSS file and merge any @import statements. - Whitespace compress and comment-strip the CSS ...
7
by: Raj | last post by:
Hi I was hoping someone could suggest a simple way of stripping non-numeric data from a string of numbers. For example, if I have "ADB12458789\n" I would like to remove the letters and the...
6
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
6
by: Medros | last post by:
I understand that you can strip html out of a txt file so that all the information is left is the visable information that is needed (e.g. everything that has < > around is gone). My question is...
4
by: Tim Cook | last post by:
Hi All, I just ran into an issue with the rstrip method when using it on path strings. When executing a function I have a need to strip off a portion of the current working directory and add...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.