AttributeError:'str'对象没有属性'sub'Python代码

时间:2012-11-02 04:05:19

标签: python regex attributeerror

尝试学习即将到来的期末考试时遇到此错误,需要了解它为什么不起作用。这是代码。

morning_agenda = "At 9.00AM the project team will assemble. The first topic will be fixing the bug in program product.py. We'll break for coffee at 10.30. Work will then continue on improving display.html until 12.30PM."

print morning_agenda

morning_agenda.sub('([0-9]+)\.([0-9]+)', r'\1:\2', morning_agenda)

print morning_agenda

1 个答案:

答案 0 :(得分:7)

re.sub是re模块中的函数,而不是字符串的方法。

import re
morning_agenda = re.sub('([0-9]+)\.([0-9]+)', r'\1:\2', morning_agenda)