为什么在使用字典时translate()会给我TypeError: expected a character buffer object
错误?在
remap = {
'with': 'TEXT1',
'as': 'TEXT2',
'text_in': 'TEXT3'
}
s = "with open(loc_path + 'sample_text.txt', 'rb') as text_in:"
ss = s.translate(remap)
print ss
以下是错误消息:
^{pr2}$使用replace()可以:
#ss = s.translate(remap)
#print ss
s = s.replace('with', 'TEXT1')
s = s.replace('as', 'TEXT2')
s = s.replace('text_in', 'TEXT3')
print s
输出:
TEXT1 open(loc_path + 'sample_text.txt', 'rb') TEXT2 TEXT3:
[Finished in 0.1s]
来自documentation(重点矿):
在您的代码中,
remap
不满足此要求。在相关问题 更多 >
编程相关推荐