Python list index out of range on return value of split

前端 未结 4 712
南笙
南笙 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条回答
  •  一个人的身影
    2021-01-18 11:53

    I would guess you have a blank line somewhere in your file. If it runs through the data and then generates the exception the blank line will be at the end of your file.

    Please insert

    print len(line), line
    

    before your

    print line[1]
    

    as a check to verify if this is the case.

    You can always use this construct to test for blank lines and only process/print non-blank lines:

    for line in f:
        line = line.strip()
        if line:
           # process/print line further
    

提交回复
热议问题