[Tutor] Finding and replacing a space in a string.
peterabrown at froggy.com.au
Wed Jan 28 16:54:07 EST 2004
At 16:36 28/01/04 -0500, you wrote:
>Hey guys and gals...
>I'm working with the csv module...I have a row which is a ID.
>let's call ID = row.
>My problem is that row or ID is 6 characters or less and in some
>casses it has a ' ' space which i need to replace with a '-' and if it
>does not have a space I would like to leave it alone.
>Any sugestions ?
> >>> id
>newid should become 'ZURE-A'
>and if id = 'WHITO' newid = 'WHITO'
>>> id ='ZUE A'
>>> id1= id1.replace(' ','-')
>>> id= id.replace(' ','-')
In other words if id doesn't have a space it doesn't treat the command as
an error. So you are free to apply to every line, where there is a space it
will replace it, otherwise id is not chnaged.
>Thanks for the help :)
>Learning python and loving it !
>Tutor maillist - Tutor at python.org
More information about the Tutor