简体   繁体   English

空列表的 Python 和 Pandas NoneType 错误

[英]Python and Pandas NoneType error for empty list

I have a form which takes a list of inputs via html (searchinput) and adds it to a query in python.我有一个表单,它通过 html (searchinput) 获取输入列表并将其添加到 python 中的查询中。

    searchterms = []
    searchterms = request.values.get('searchinput').strip().split(', ')
    filtered = pd.concat([df.query("Drug.str.contains('|'.join(@searchterms), na=False, case=False, regex=True)", engine='python')])
    return render_template('drugsafety.html', tables=[filtered.to_html(classes='data')], titles=['na', 'Drug List'])

This gives这给

AttributeError: 'NoneType' object has no attribute 'strip' AttributeError: 'NoneType' 对象没有属性 'strip'

If I then feed in a value like searchterms = ["foobar"] it works and will even take the form inputs without error.如果我输入一个像 searchterms = ["foobar"] 这样的值,它就可以工作,甚至可以毫无错误地接受表单输入。

How can I get around this initial needing a value to boot it into life?我怎样才能解决这个最初需要一个价值来启动它的生活?

edit编辑

    searchterms = []
    if searchterms is not None:
        searchterms = request.values.get('searchinput').strip().split(', ')
        filtered = pd.concat([ df.query("Drug.str.contains('|'.join(@searchterms), na=False, case=False, regex=True)", engine='python')])            
    else:  
        searchterms = ["Foo"]

Thanks for any help谢谢你的帮助

Try to put default value ( '' ) in .get :尝试将默认值 ( '' ) 放入.get

# put `''` in .get():
searchterms = request.values.get('searchinput', '').strip().split(', ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM