我有这段代码可以将一些字符串打印到文本文件中,但是我需要 python 来忽略每个空项,因此它不会打印空行。
我写了这段代码,很简单,但应该可以解决问题:
lastReadCategories = open('c:/digitalLibrary/' + connectedUser + '/lastReadCategories.txt', 'w')
for category in lastReadCategoriesList:
if category.split(",")[0] is not "" and category is not None:
lastReadCategories.write(category + '\n')
print(category)
else: print("/" + category + "/")
lastReadCategories.close()
我看不出它有什么问题,但是,python 不断将空项目打印到文件中。所有类别都用这种表示法编写:“category,timesRead”,这就是为什么我要求python查看逗号之前的第一个字符串是否不为空。然后我看看整个项目是否不为空(不是无)。从理论上讲,我想它应该可以工作,对吧?
PS:我已经尝试询问 if 来检查“类别”是否不是“”并且不是“”,仍然是相同的结果。