string.translate 上的预期缓冲区对象错误 - python 2.6
我希望为 python 新手提供一些帮助,我正在尝试从字符串中删除一些字符,例如:
string1 = "100.000"
deleteList = [",", "."]
string1.translate(None, deleteList)
print string1
但我收到 TypeError: Expected a character buffer object
为什么我这样做收到此错误,它引用哪个参数?我在哪里可以找到这方面的帮助。 我在 Windows 上使用 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
string.translate
的文档说明了哪个建议
deletechars
应该是一个字符串,而不是一个字符列表:The docs for
string.translate
sayswhich suggests that
deletechars
should be a string of characters, instead of a list of characters:您收到的错误涉及您的
deleteList
变量,它应该是一个字符串。如果您确实需要将字符存储在列表中,您可以这样做: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: