How to check if the string is empty?

后端 未结 25 1931
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:47

Does Python have something like an empty string variable where you can do:

if myString == string.empty:

Regardless, what\'s the most elegan

25条回答
  •  再見小時候
    2020-11-22 15:26

    In case this is useful to someone, here is a quick function i built out to replace blank strings with N/A's in lists of lists (python 2).

    y = [["1","2",""],["1","4",""]]
    
    def replace_blank_strings_in_lists_of_lists(list_of_lists):
        new_list = []
        for one_list in list_of_lists:
            new_one_list = []
            for element in one_list:
                if element:
                    new_one_list.append(element)
                else:
                    new_one_list.append("N/A")
            new_list.append(new_one_list)
        return new_list
    
    
    x= replace_blank_strings_in_lists_of_lists(y)
    print x
    

    This is useful for posting lists of lists to a mysql database that does not accept blanks for certain fields (fields marked as NN in schema. in my case, this was due to a composite primary key).

提交回复
热议问题