Python not ignoring empty items in list -
i have code print strings text file, need python ignore every empty items, doesn't print empty lines.
wrote code, simple, should trick:
lastreadcategories = open('c:/digitallibrary/' + connecteduser + '/lastreadcategories.txt', 'w') category in lastreadcategorieslist: if category.split(",")[0] not "" , category not none: lastreadcategories.write(category + '\n') print(category) else: print("/" + category + "/") lastreadcategories.close() i can see no problem it, yet, python keeps printing empty items file. categories written in notation: "category,timesread", that's why ask python see if first string before comma not empty. see if whole item not empty (is not none). in theory guess should work, right?
p.s.: i've tried asking if check if 'category' not "" , not " ", still, same result.
rstrip() category before writing file
lastreadcategories = open('c:/digitallibrary/' + connecteduser +'/lastreadcategories.txt', 'w') category in lastreadcategorieslist: if category.split(",")[0] not "" , category not none: lastreadcategories.write(category.rstrip() + '\n') print(category.rstrip()) else: print("/" + category + "/") lastreadcategories.close() i able test sample list provided (without writing file):
lastreadcategorieslist = ['a,52', 'b,1\n', 'c,50', ',3'] category in lastreadcategorieslist: if category.split(",")[0] not "" , category not none: print(category.rstrip()) else: print("/" + category + "/") >>> ================================ restart ================================ >>> a,52 b,1 c,50 /,3/ >>>
Comments
Post a Comment