regular expresson for Unix and Dos Lineendings wanted
Franz Steinhaeusler
franz.steinhaeusler at gmx.at
Fri Feb 24 06:36:01 EST 2006
On Thu, 23 Feb 2006 17:41:47 +0000, Martin Franklin
<mfranklin1 at gatwick.westerngeco.slb.com> wrote:
>Franz Steinhaeusler wrote:
>> On Thu, 23 Feb 2006 13:54:50 +0000, Martin Franklin
>> <mfranklin1 at gatwick.westerngeco.slb.com> wrote:
>>
>>>>>>> r="erewr \r\nafjdskl "
>>>> 'erewr \r\nafjdskl'
>>>>
>>>> 2) Unix
>>>>>>> r="erewr \nafjdskl "
>>>> 'erewr\nafjdskl'
>>> why not use string methods strip, rstrip and lstrip
>>>
>>
>> because this removes only the last spaces,
>>>>> r
>> 'erewr \r\nafjdskl '
>>>>> r.rstrip()
>> 'erewr \r\nafjdskl'
>>
>> I want:
>> 'erewr\r\nafjdskl'
>>
>> or for unix line endings
>> 'erewr\nafjdskl'
>>
>
>
>how about one of these variations
>
>print 'erewr \r\nafjdskl '.replace(" ", "")
>print 'erewr \r\nafjdskl '.strip(" \t")
>
>
Version 1:
>>> w='erewr \r\nafjdskl '.replace(" ", "")
>>> w
'erewr\r\nafjdskl'
>>> w='erewr \nafjdskl '.replace(" ", "")
>>> w
'erewr\nafjdskl'
>>> w='word1 word2 \nafjdskl '.replace(" ", "")
>>> w
'word1word2\nafjdskl'
>>>
it replaces all spaces, not only the trailing whitespaces.
version 2:
>>> w = 'erewr \r\nafjdskl '.strip(" \t")
>>> w
'erewr \r\nafjdskl'
>>> w = 'erewr \nafjdskl '.strip(" \t")
>>> w
'erewr \nafjdskl'
>>> w = 'word1 word2 \nafjdskl '.strip(" \t")
>>> w
'word1 word2 \nafjdskl'
>>>
I found a solution (not the most beautiful, but for
my purpose sufficiently good.)
Given: a file has no mixed lineendings, so it is either
a dos or unix file (mac line endings not respected).
swin="erewr \r\nafjdskl "
sunix="erewr \nafjdskl "
Dos Line endings (at least on '\r' included)?
r is contents of a file:
helpchar = ''
if r.find('\r') != -1:
helpchar = '\r'
retrailingwhitespacelf = re.compile('(?<=\S)[ \t'+helpchar+']+$',
re.MULTILINE)
newtext, n = retrailingwhitespace.subn(helpchar, r)
if n > 1:
r = newtext
--
Franz Steinhaeusler
More information about the Python-list
mailing list