我有一个脚本解析真实在此文件中的第一个大写的话:解析成功直到IndexError?
IMPORT fs
IF fs.exists("fs.pyra") THEN
PRINT "fs.pyra Exists!"
END
脚本是这样的:
file = open(sys.argv[1], "r")
file = file.read().split("\n")
while '' in file:
findIt = file.index('')
file.pop(findIt)
for line in file:
func = ""
index = 0
while line[index] == " ":
index = index + 1
while not line[index] == " " or "=" and line[index].isupper():
func = func + line[index]
index = index + 1
print func
所有使用的模块已经导入。
我通过正在解析的路径参数的文件,我得到这样的输出:
IMPORT
IF
PRINT
Traceback (most recent call last):
File "src/source.py", line 20, in <module>
while not line[index] == " " or "=" and line[index].isupper():
IndexError: string index out of range
这意味着它的成功解析,直到在列表中的最后一个参数,然后它不解析它在所有。我该如何解决?
'而不用排队[指数] ==“”或“=”'不是在做你认为的事情 - 你可能是指'in('=')'而不是? –
'file.read()。split(“\ n”)'...?为什么不'file.readlines()'? –
@ cricket_007'file.readlines()'在其中留下换行符。 – baranskistad