I´d appreciate some help for a python novice, I´m trying to delete some characters from a string, like this, for example:
string1 = "100.000"
deleteList = [",", "."]
string1.translate(None, deleteList)
print string1
but I get a TypeError: expected a character buffer object
Why do I get this error, which argument does it refer to? and where can i find help on this. I'm using python 2.6 on windows.
The docs for string.translate
says
S.translate(table [,deletechars]) -> string
which suggests that deletechars
should be a string of characters, instead of a list of characters:
string1 = "100.000"
string1=string1.translate(None, ',.')
print (string1)
# 100000
The error you get reffers to your deleteList
variable, it should be a string. If you really need to store the chars in a list, you could do this:
string1.translate(None, ''.join(deleteList))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With