[Tutor] Filtering a String

Matt Williams matthew.williams at cancer.org.uk
Sun Mar 20 23:05:23 CET 2005


Dear List,

I'm trying to filter a file, to get rid of some characters I don't want
in it.

I've got the "Python Cookbook", which handily seems to do what I want,
but:

a) doesn't quite and 
b) I don't understand it

I'm trying to use the string.maketrans() and string.translate(). From
what I've read (in the book and the Python Docs), I need to make a
translation table, (using maketrans) and then pass the table, plus and
optional set of characters to be deleted, to the translate() function.

I've tried:

#!/usr/bin/python
import string 
test="1,2,3,bob,%,)"
allchar=string.maketrans('','')
#This aiming to delete the % and ):
x=''.translate(test,allchar,"%,)")

but get:
TypeError: translate expected at most 2 arguments, got 3

Please could someone explain this to me (slowly).
As a measure of my slowness:
This is my first programming language
I couldn't get the tutor list to work for ages - until I realised I was
sending all the desperate pleas for help to tutor-request. Perhaps there
is no hope......

Matt Williams




More information about the Tutor mailing list