473,372 Members | 1,000 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,372 software developers and data experts.

compressing consecutive spaces

How can I replace multiple consecutive spaces in a file with a single
character (usually a space, but maybe a comma if converting to a CSV
file)? Ideally, the Python program would not compress consecutive
spaces inside single or double quotes. An inelegant method is to
repeatedly replace two consecutive spaces with one.

Jul 9 '07 #1
2 2931
On Jul 9, 9:38 am, Beliavsky <beliav...@aol.comwrote:
How can I replace multiple consecutive spaces in a file with a single
character (usually a space, but maybe a comma if converting to a CSV
file)? Ideally, the Python program would not compress consecutive
spaces inside single or double quotes. An inelegant method is to
repeatedly replace two consecutive spaces with one.
Split with no arguments splits on whitespace, and multiple spaces
count as but a single separator. So split+join = collapsed
whitespace.
>>test = "a b c d efdd slkj sdfdsfl"
" ".join(test.split())
'a b c d efdd slkj sdfdsfl'

Or use something other than " " to join with, such as ",".
>>",".join(test.split())
'a,b,c,d,efdd,slkj,sdfdsfl'

-- Paul

Jul 9 '07 #2
On Jul 9, 7:38 am, Beliavsky <beliav...@aol.comwrote:
How can I replace multiple consecutive spaces in a file with a single
character (usually a space, but maybe a comma if converting to a CSV
file)? Ideally, the Python program would not compress consecutive
spaces inside single or double quotes. An inelegant method is to
repeatedly replace two consecutive spaces with one.


One can try mx.TextTools. E.g.,
from mx.TextTools import *
import re

string_inside_quotes=re.compile(r'(?P<quote>["\']).*?(?<!\\)(?
P=quote)',
re.MULTILINE)

def advance_position(text, position, len_text, sre):
mobj = sre.match(text[position:])
if mobj:
incr = len(mobj.group(0))
else:
incr = 0
return position + incr
table = ('try_again',
('quoted_string', CallArg,
(advance_position, string_inside_quotes), +1,
'try_again'),
('nonspace', AllNotIn, ' ', +1, 'try_again'),
('space', AllIn, ' ', +1, 'try_again'),
(None, EOF, Here, +1, MatchOk),
(None, Fail, Here),)

for target_string in (
" Try using mx.TextTools 'for parsing strings'",
"'It might be' just what you needed",
'I find "it worthwhile"',
):
print "BEFORE:%s" % target_string
_, taglist, _ = tag(target_string, table)
if taglist:
tokens = []
for t in taglist:
tagobj, left_index, right_index = t[0:3]
if tagobj == 'space':
tokens.append(' ')
else:
tokens.append(target_string[left_index:right_index])
print "AFTER:%s" % ''.join(tokens)
else:
print "Something went horribly wrong"
--
Hope this helps,
Steven

Jul 9 '07 #3

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

Similar topics

8
by: Adam | last post by:
Hi, I am trying to mark consective numbers in a data set and get a count as to how many consecutive numbers exist in the a given line of data. Here is an example line: 3, 5, 7, 9, 10, 13,...
7
by: Jim Vorbau | last post by:
I would like to change the colors on consecutive letters of a word. When adding a style associated with the span tag (see below), there is a gap after each letter. Does anyone know why the span tag...
22
by: Kim Scarborough | last post by:
Say I wanted to put a couple lines of asterisks between paragraphs, and have it stretch the spaces so that it spanned 100% of the text-width, like this: * * * * * ...
0
by: Yuancai \(Charlie\) Ye | last post by:
Hi, All: I am happy to annouce that we have formally released our latest SocketPro version 4 at www.udaparts.com, an advanced remoting framework written from batching/queue, asynchrony and...
6
by: sri2097 | last post by:
Hi, I'm trying to zip a particular fiolder and place the zipped folder into a target folder using python. I have used the following command in 'ubuntu'. zip_command = 'zip -qr %s %s' % (target,...
1
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a service running on a main server. What i want to do is to compress the files before i send them to an FTP site. Is there a built in compression tool in the .NET Framework 2.0 or...
7
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: ...
6
by: cesco | last post by:
Hi, say I have a vector like the following: vec = and I'd like to know via a function (for example, ConsecutiveIntegers(vec, N)) whether there are N consecutive integers. So, for example,...
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.