【问题标题】:AttributeError: 'str' object has no attribute 'sub' Python codeAttributeError: 'str' 对象没有属性 'sub' Python 代码
【发布时间】:2012-10-22 18:04:58
【问题描述】:

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

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

【问题讨论】:

    标签: python regex attributeerror


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 2015-09-30
      • 2021-10-22
      • 2020-09-22
      • 2020-06-25
      • 2016-12-27
      • 2015-03-08
      • 1970-01-01
      相关资源
      最近更新 更多