我有一個腳本解析真實在此文件中的第一個大寫的話:解析成功直到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