python去除读取文件中多余的空行
今天在写登录程序练习的时候,黑名单文件中多了几行空行。导致运行的时候报错:IndexError: list index out of range
代码
brackData = open(brackDataPath,"r") for line in brackData: temp = line.strip().split(',') brackDict[temp[0]] = int(temp[1]) brackData.close()
错误
Traceback (most recent call last): File "D:/新建文件夹/DAY1/loginApp/loginApp.py", line 31, in <module> brackDict[temp[0]] = int(temp[1]) IndexError: list index out of range
调试将temp打印出来,发现是由于多余的空行temp = [""]
修改程序如下后就好了
brackData = open(brackDataPath,"r") for line in brackData: temp = line.strip().split(',') if len(temp)> 1: brackDict[temp[0]] = int(temp[1]) brackData.close()