|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
removing unwanted characters from a string
I was wondering how to remove a character from a string that was retreived from a text file (.txt).
|
|
#2
|
|||
|
|||
|
If you only want to remove all instances of a single character, then you can use the string replace method:
Code:
>>> txt = 'hello world'
>>> txt.replace('l', '')
'heo word'
To remove a set of characters then you could use the filter function: Code:
>>> filter(lambda x: x not in 'aeiou', txt) 'hll wrld' Alternatively you could use the string module's translate and maketrans functions: Code:
>>> import string
>>> trans = string.maketrans('', '')
>>> string.translate(txt, trans, 'aeiou')
'hll wrld'
The translate function is more useful if you wish to replace some characters and remove others. EDIT: translate is also much faster than filter, since it is implemented entirely in C, while filter will need to call a python function for each character. This probably does not matter much for scripts occasionally acting on short strings, but can be critical if performance is an issue. Dave - The Developers' Coach Last edited by DevCoach : May 8th, 2004 at 06:04 PM. |
|
#3
|
|||
|
|||
|
It all depends which characters you want to remove...
To remove whitespace characters (space, tabs, newlines, etc) from the left end of the string: Code:
>>> txt = " Hello World " >>> txt.lstrip() "Hello World " From the right of the string: Code:
>>> txt = " Hello World " >>> txt.rstrip() " Hello World" From both sides: Code:
>>> txt = " Hello World " >>> txt.strip() "Hello World" To remove the first character: Code:
>>> txt = "Hello World" >>> txt[1:] "ello World" The last character: Code:
>>> txt = "Hello World" >>> txt[:-1] "Hello Worl" The 5th character (or the nth character(s)): Code:
>>> txt = "Hello World" >>> txt[:5] + t[6:] "HelloWorld" ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > removing unwanted characters from a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|