Python str对象没有属性'\uu getattr\uuu'

2024-09-26 22:55:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉,我就是不知道她怎么了:

def SomeDato(): 
  today = datetime.date.today()
  todayStr = idag.strftime('%d.%m.%y')  
  return todayStr

def AnotherFunctionWithinTestComplete()
  strArg = SomeDato()
  datoToCheck = dato.contentText #dato.contentText returns "13.12.15. 16:29"
  if datoToCheck.startswith(strArg):
    do something here....

我得到了这个错误:str object has no atribute '__getattr__'if datoToCheck.startswith(strArg)行中。你知道吗


Tags: todaydatetimedatereturnifdefdatostrftime
2条回答

抱歉,我的代码是丹麦语,显然我没有翻译所有的东西。现在应该可以了:

#returns 13.12.15
def SomeDate(): 
  today = datetime.date.today()
  todayStr = today.strftime('%d.%m.%y')  
  return todayStr

def AnotherFunctionWithinTestComplete()
  # finds an element in the DOM tree 
  date = Aliases.browser.MyPage.FindChildByXPath("some xPath") 
  strArg = SomeDate()
  #date.contentText returns "13.12.15 16:29"
  dateToCheck = date.contentText
  if dateToCheck.startswith(strArg):
    do something here....

我刚刚意识到,如果我这样写:

def AnotherFunctionWithinTestComplete()
  # finds an element in the DOM tree 
  date = Aliases.browser.MyPage.FindChildByXPath("some xPath") 
  strArg = SomeDate()
  #date.contentText returns "13.12.15 16:29"
  dateToCheck = date.contentText + "" 
  if dateToCheck.startswith(strArg):
    do something here....

那就成功了。所以dateToCheck=日期.contentText+“”而不是dateToCheck=日期.contentText有区别。有什么区别?你知道吗

strftimedatetime类的一个方法(在datetime模块中定义得有些混乱),因此用

from datetime import datetime

datetime.strftime(today, '%d.%m.%y')

其中today是日期时间类型。这也可以用today.strftime('%d.%m.%y')作为实例方法调用。在你的例子中idag是什么?错误消息表明它是一个字符串。如果要将idag转换为datetime,可以通过datetime.strptime(idag, '%d.%m.%y')实现,前提是它已经是'%d.%m.%y'格式。你知道吗

相关问题 更多 >

    热门问题