Python list index out of range on return value of split

前端 未结 4 701
南笙
南笙 2021-01-18 11:22

I\'m writing a simple script that is trying to extract the first element from the second column of a .txt input file.

import sys

if (len(sys.argv) > 1):         


        
4条回答
  •  Happy的楠姐
    2021-01-18 12:00

    It can be useful to catch the exception an print the offending lines

    for line in f:
        line = line.strip("\n ' '")
        line = line.split(",") 
        try:
            print line[1]
        except IndexError, e:
            print e
            print "line =", line
            raise   # if you don't wish to continue
    

提交回复
热议问题