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()

  

posted on 2016-04-10 20:16  那个踩到香蕉皮的妖怪  阅读(3803)  评论(0编辑  收藏  举报