AttributeError: ‘NoneType’ object has no attribute ‘strip’

  • Post author:
  • Post category:其他




删除None或者空字符串时出错

def is_not_empty(s):

    return len(s.strip()) > 0

print(list(filter(is_not_empty, ['hahah', 'jiayou', 'keyide', ' ', None, 'none'])))

解决办法:

# 删除None或者空字符串
def is_not_empty(s):

    return s and len(s.strip()) > 0   # 在此处进行了修改

print(list(filter(is_not_empty, ['hahah', 'jiayou', 'keyide', ' ', None, 'none'])))

结果为:

[‘hahah’, ‘jiayou’, ‘keyide’, ‘none’]



版权声明:本文为weixin_43283397原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。