【问题标题】:Attribute Error 'NoneType' object has no attribute属性错误“NoneType”对象没有属性
【发布时间】:2021-06-11 11:17:15
【问题描述】:

我一直在尝试这段代码,但我一直得到相同的结果,即:

如果 Prep.endswith ('jpg'): AttributeError:'NoneType' 对象没有属性 'endswith'

  import re
result = "hey(14).jpg"

Prep = print(re.sub(r" ?\([^)]+\)", "", result))

if Prep.endswith ('jpg'):
    Caption = print(Prep.replace("jpg", "is jpg"))
elif Prep.endswith ('png'):
    Caption = print(Prep.replace("png", "is png"))
elif Prep.endswith ('gif'):
    Caption = print(Prep.replace("gif", "is gif"))
else :
    Caption = print("Unknown")

我不知道这是从哪里来的。

我们将不胜感激。

干杯。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    问题出在这里:

    Prep = print(re.sub(r" ?\([^)]+\)", "", result))
    

    您将print 的返回值分配给Prep,然后尝试使用它。问题是print 没有返回任何东西——换句话说,它总是返回None

    你想要这个:

    Prep = re.sub(r" ?\([^)]+\)", "", result)
    print(Prep)
    

    【讨论】:

    • 是的,就是这样,因为我是编码和学习“艰难方式”的新手,我不知道这一点,非常感谢您的帮助
    猜你喜欢
    • 2017-09-14
    • 2021-02-17
    • 2023-02-17
    • 2018-05-02
    • 2019-03-17
    • 1970-01-01
    • 2022-11-29
    • 2021-09-17
    • 2020-10-22
    相关资源
    最近更新 更多