索引器错误:列表索引超出范围Python错误

索引器错误:列表索引超出范围Python错误,python,Python,我的命令行参数: python SearchString.py 10 nee 参数1与长度不匹配。我该怎么处理 错误: 字符串搜索中第30行的文件“SearchString.py” 搜索=临时[字段索引] 索引器:列表索引超出范围 #!usr/bin/python import sys def string_search(): ''' This function search a string in a file through index and gives the resu

我的命令行参数:

python SearchString.py 10 nee

参数1与长度不匹配。我该怎么处理

错误: 字符串搜索中第30行的文件“SearchString.py” 搜索=临时[字段索引] 索引器:列表索引超出范围

#!usr/bin/python
import sys

def string_search():
    '''
    This function search a string in a file through index and gives the result.

    :returns: none
    :return type: none 
    :author:XYZ   

    '''
    if len(sys.argv) != 3:
        print "Enter Two Arguments Only"
        sys.exit()

    stringsrch = sys.argv[2]
    found = False
    file_name = open("passwd", "r")

    if sys.argv[1].isdigit():
        fieldindex = int(sys.argv[1])-1
    else:
       print "Enter Integer in 1st Argument"
    sys.exit()
    #fieldindex = int(sys.argv[1])-1

   for store_file in file_name:
        temp = store_file.split(":")
        search = temp[fieldindex]

        if stringsrch in search:
            print store_file
            found = True
    if not found:
        print "No String "

string_search()

我认为代码取决于你如何使用它。
search=temp[fieldindex]
。根据代码,您的字段索引是
9
,因此您应该确保
len(temp)>9
。或者你会得到像你所说的那样的错误。

如果fieldindex>=len(temp):有错误消息,为什么不在for循环中的带有错误消息的行之前,
search=temp[fieldindex]
?我是一个新手我是一个新手并不是思考你正在写的代码的借口