Estou recebendo new_tag
de um campo de texto de formulário com self.response.get("new_tag")
e selected_tags
de campos de caixa de seleção com
self.response.get_all("selected_tags")
Eu combino assim:
tag_string = new_tag new_tag_list = f1.striplist(tag_string.split(";") + selected_tags)
(f1.striplist
é uma função que remove os espaços em branco dentro das strings em a lista.)
Mas no caso de tag_list
estar vazio (nenhuma nova tag foi inserida), mas existem algumas selected_tags
, new_tag_list
contém uma string vazia " "
.
Por exemplo, de logging.info
:
new_tag selected_tags[u"Hello", u"Cool", u"Glam"] new_tag_list[u"", u"Hello", u"Cool", u"Glam"]
Como me livrar da string vazia?
Se houver uma string vazia na lista:
>>> s = [u"", u"Olá", u"Cool", u"Glam"] >>> i = s.index("") >>> del s[i] >>> s [u"Hello", u"Cool", u"Glam"]
Mas se não houver nenhuma string vazia:
>> > s = [u"Olá", u"Cool", u"Glam"] >>> if s.index(""): i = s.index("") del s[i] else: print "new_tag_list não tem string vazia"
Mas isso dá:
Traceback (última chamada mais recente): Arquivo "<pyshell#30>", linha 1, em <module> if new_tag_list.index(""): ValueError: list.index(x): x não está na lista
Por que isso acontece e como faço para contornar isso?