解析成功直到IndexError?
问题描述:
我有一个脚本解析真实在此文件中的第一个大写的话:解析成功直到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
这意味着它的成功解析,直到在列表中的最后一个参数,然后它不解析它在所有。我该如何解决?
答
您不需要增加空间上的索引 - line.strip()
将删除前导和尾随空格。
您可以在split()
空格处获得单词。
然后你可以遍历这些字符串,并使用isupper()
检查整个单词,而不是单个字符
另外,运行通过模式匹配整个文件[A-Z]+
不管怎么说,你的错误...
while not line[index] == " " or "="
or "="
始终为真,因此您的索引超出范围
答
如果您要处理的文件与Python内置的标记器兼容,则可以使用该标记,以便它可以处理引号内的内容,然后采取非常第一名令牌它在首都发现从每一行中,例如:
import sys
from itertools import groupby
from tokenize import generate_tokens, NAME
with open(sys.argv[1]) as fin:
# Tokenize and group by each line
grouped = groupby(tokenize.generate_tokens(fin.readline), lambda L: L[4])
# Go over the lines
for k, g in grouped:
try:
# Get the first capitalised name
print next(t[1] for t in g if t[0] == NAME and t[1].isupper())
except StopIteration:
# Couldn't find one - so no panic - move on
pass
这给你:
IMPORT
IF
PRINT
END
'而不用排队[指数] ==“”或“=”'不是在做你认为的事情 - 你可能是指'in('=')'而不是? –
'file.read()。split(“\ n”)'...?为什么不'file.readlines()'? –
@ cricket_007'file.readlines()'在其中留下换行符。 – baranskistad