473,405 Members | 2,282 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,405 software developers and data experts.

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

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
Jul 21 '05 #1
8 1286
Ric Da Force wrote:
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

Use rfind and slicing:
x = "C1, C2, C3"
x[:x.rfind(',')]+' and'+x[x.rfind(',')+1:]

'C1, C2 and C3'

HTH,
Wolfram
Jul 21 '05 #2
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
Jul 21 '05 #3
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']


Jul 21 '05 #4
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
Jul 21 '05 #5
Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach! Again, I think that it speaks loudly for the concise way of doing
thins in Python... Anyway, I have typed in all of the solutions and have
gained a great understanding of how to do this in future.

Thanks again!

Ric
"Brian van den Broek" <bv****@po-box.mcgill.ca> wrote in message
news:ma***************************************@pyt hon.org...
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

Jul 21 '05 #6
On Wed, 13 Jul 2005 03:47:07 +0800, "Ric Da Force" <ri*@next-level.com.au> wrote:
Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach! Again, I think that it speaks loudly for the concise way of doing
thins in Python... Anyway, I have typed in all of the solutions and have
gained a great understanding of how to do this in future.

Thanks again!

Ric
"Brian van den Broek" <bv****@po-box.mcgill.ca> wrote in message
news:ma***************************************@py thon.org...
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
Or, to finish Brian's solution by inserting the ", and" in place of the "," :
data = "the first bit, then the second, finally the third"
', and'.join(data.rsplit(',',1)) 'the first bit, then the second, and finally the third' ', and'.join('C1, C2, C3'.rsplit(',',1))

'C1, C2, and C3'

Regards,
Bengt Richter
Jul 21 '05 #7
Ric Da Force wrote:
Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach!


Just for divergence, you can also do this with regular expressions:
import re
re.sub("(.*),(.*)", r"\1 and\2", "C1, C2, C3")

'C1, C2 and C3'

Alan.

Jul 21 '05 #8
Ric Da Force <ri*@next-level.com.au> 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
Jul 21 '05 #9

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

Similar topics

1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
4
by: striker | last post by:
I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example: one, two, three,,,,four,five,,,,six one, two,...
7
by: JAY | last post by:
There is an example which confuses me, int main() { int a,b,c,d; a=3; b=5; c=a,b; d=(a,b); printf("c=%d",c); printf("d=%d",d);
5
by: Denis Petronenko | last post by:
Hello, for(int i=0; i<100; ++i){ cout << i << ","; } // how can i remove last comma here? ...... cout << endl;
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
7
sumittyagi
by: sumittyagi | last post by:
Hi all, In ksh script, I have a comma separated list. I want to replace every third comma with newline. Please assist me in this. Thanks a lot!
12
by: sivadhanekula | last post by:
Hi everyone I have a database of 24 tables all with 'dot separated numbers like 39.6' and I am extracted the required data in to one table which is also a dot separated data. But I need that data...
8
by: E11esar | last post by:
Hi there. I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes. For example: "The", "cat", "sat", "on, the", "mat" Here...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.