Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-13 Thread Florian Diesch
Ric Da Force [EMAIL PROTECTED] wrote:
 I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
 text is of fixed size, what is the easiest way to change this list so that 
 it reads:
 'C1, C2 and C3' regardless of the length of the string.

 import re
 data = the first bit, then the second, finally the third
 re.sub(r(.*),, r\1 and, data)
'the first bit, then the second and finally the third'
 


   Florian
-- 
begin  signature_virus
 Hi! I'm a signature virus. Please copy me to your signature to help me spread.
end
-- 
http://mail.python.org/mailman/listinfo/python-list


Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-12 Thread Ric Da Force
Hi,

I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
text is of fixed size, what is the easiest way to change this list so that 
it reads:
'C1, C2 and C3' regardless of the length of the string.

Regards and sorry for the newbie question,

Ric 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-12 Thread Peter Otten
Ric Da Force wrote:

 I have a string such as 'C1, C2, C3'.   Without assuming that each bit of
 text is of fixed size, what is the easiest way to change this list so that
 it reads:
 'C1, C2 and C3' regardless of the length of the string.

  and.join(C1, C2, C3.rsplit(,, 1))
'C1, C2 and C3'

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-12 Thread Christoph Rackwitz
foo = C1, C2, C3
foo = foo.split(, ) # ['C1', 'C2', 'C3']
foo = , .join(foo[:-1]) +  and  + foo[-1] # just slicing and
joining it

you can always look for something here:
http://docs.python.org/lib/lib.html
and there:
http://docs.python.org/ref/

if you want to know, what methods an object has, try this (i shortened
the output a bit):
 foo = C1, C2, C3
 dir(foo)
['__add__', '__doc__', ..., 'capitalize', 'center', 'count',
'decode','encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind',
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-12 Thread Brian van den Broek
Ric Da Force said unto the world upon 12/07/2005 02:43:
 Hi,
 
 I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
 text is of fixed size, what is the easiest way to change this list so that 
 it reads:
 'C1, C2 and C3' regardless of the length of the string.
 
 Regards and sorry for the newbie question,
 
 Ric 
 

Hi Ric,

the rsplit method of strings should get you going:

  data = the first bit, then the second, finally the third
  chunks = data.rsplit(',', 1)
  chunks
['the first bit, then the second', ' finally the third']
 

Best,

Brian vdB


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-12 Thread Cyril Bazin
If that can help you...

def replaceLastComma(s):
 i = s.rindex(,)
 return ' and'.join([s[:i], s[i+1:]])
 
I don't know of ot's better to do a:
' and'.join([s[:i], s[i+1:]])
Or:
''.join([s[:i], ' and', s[i+1:]])
Or:
s[:i] + ' and' + s[i+1]
Maybe the better solution is not in the list...

Cyril
On 7/12/05, Ric Da Force [EMAIL PROTECTED] wrote:
Hi,I have a string such as 'C1, C2, C3'. Without assuming that each bit oftext is of fixed size, what is the easiest way to change this list so thatit reads:'C1, C2 and C3' regardless of the length of the string.
Regards and sorry for the newbie question,Ric--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list