Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 492
CCID Avatar asked Aug 09 '11 20:08

CCID


2 Answers

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
like image 198
unutbu Avatar answered Oct 24 '22 05:10

unutbu


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))
like image 30
mdeous Avatar answered Oct 24 '22 06:10

mdeous