我的命令行参数:
python SearchString.py 10 nee
参数1与长度不匹配。我该怎么处理?
错误: 在string_search中输入第30行的“SearchString.py” search = temp [fieldindex] IndexError:列表索引超出范围
#!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()
答案 0 :(得分:0)
我认为代码取决于您如何使用它。
search = temp[fieldindex]
。从代码中,您的fieldindex为9
,因此您应该确保len(temp) > 9
。或者你会得到像你所说的错误。