CCID
CCID

Reputation: 1448

expected buffer object error on string.translate - python 2.6

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.

Upvotes: 5

Views: 4927

Answers (2)

mdeous
mdeous

Reputation: 18039

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))

Upvotes: 1

unutbu
unutbu

Reputation: 880199

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

Upvotes: 10

Related Questions