Python 3-索引器错误:字符串索引超出范围
我目前正在用Python3.6创建一种编程语言,出于某种原因,下面的代码生成一个索引器:字符串索引超出范围 当我尝试在Windows批处理文件中执行以下代码时:Python 3-索引器错误:字符串索引超出范围,python,python-3.x,batch-file,Python,Python 3.x,Batch File,我目前正在用Python3.6创建一种编程语言,出于某种原因,下面的代码生成一个索引器:字符串索引超出范围 当我尝试在Windows批处理文件中执行以下代码时: @echo off python run-file.py test.ros pause 但我得到了以下输出: Traceback (most recent call last): File "run-file.py", line 16, in <module> if not(value[1][0] == "!"
@echo off
python run-file.py test.ros
pause
但我得到了以下输出:
Traceback (most recent call last):
File "run-file.py", line 16, in <module>
if not(value[1][0] == "!") and ignoreline == False:
IndexError: string index out of range
Press any key to continue . . .
syntax.py文件如下所示:
from sys import argv as args
from sys import exit as quit
import syntax
try:
args[1]
except IndexError:
print("ERROR: No ROS Code file provided in execution arguments")
print("Ensure the execution code looks something like this: python run-file.py test.ros")
with open(args[1]) as f:
ignoreline = False
content = f.readlines()
content = [x.strip() for x in content]
for value in enumerate(content):
if not(value[1][0] == "!") and ignoreline == False:
firstpart = value[1].split(".")[0]
lenoffirstpart = len(value[1].split(".")[0])
afterpart = str(value[1][lenoffirstpart + 1:])
apwithcomma = afterpart.replace(".", "', '")
preprint = str(firstpart + "(" + apwithcomma + ")")
printtext = preprint.replace("(", "('")
lastprinttext = printtext.replace(")", "')")
try:
exec(str("syntax." + lastprinttext))
except Exception as e:
template = "ERROR: An error of type {0} occured while running line {1} because {2}"
message = template.format(
type(e).__name__, str(value[0] + 1), str(e.args[0]))
print(message)
quit(1)
elif content[value[0]][0] == "!!!":
ignoreline = not(ignoreline)
quit(0)
def print_message(contents=''):
print(contents)
! This is a single line comment
!!!
This line should be ignored
and this one as well
!!!
print_message.Hello World
test.ros文件如下所示:
from sys import argv as args
from sys import exit as quit
import syntax
try:
args[1]
except IndexError:
print("ERROR: No ROS Code file provided in execution arguments")
print("Ensure the execution code looks something like this: python run-file.py test.ros")
with open(args[1]) as f:
ignoreline = False
content = f.readlines()
content = [x.strip() for x in content]
for value in enumerate(content):
if not(value[1][0] == "!") and ignoreline == False:
firstpart = value[1].split(".")[0]
lenoffirstpart = len(value[1].split(".")[0])
afterpart = str(value[1][lenoffirstpart + 1:])
apwithcomma = afterpart.replace(".", "', '")
preprint = str(firstpart + "(" + apwithcomma + ")")
printtext = preprint.replace("(", "('")
lastprinttext = printtext.replace(")", "')")
try:
exec(str("syntax." + lastprinttext))
except Exception as e:
template = "ERROR: An error of type {0} occured while running line {1} because {2}"
message = template.format(
type(e).__name__, str(value[0] + 1), str(e.args[0]))
print(message)
quit(1)
elif content[value[0]][0] == "!!!":
ignoreline = not(ignoreline)
quit(0)
def print_message(contents=''):
print(contents)
! This is a single line comment
!!!
This line should be ignored
and this one as well
!!!
print_message.Hello World
问题似乎出现在run-file.py文件的第16行:
我已经尝试过用值[1][0]替换值[1][0],并用括号替换其他组合,但都没有用
似乎当我尝试打印值时,它的行为与预期一致,并给了我!这是test.ros文件的第一个字符,但由于某种原因,它在if语句中会引发异常
如果您想要更多的源代码,可以在Github上找到包含所有文件的确切提交
更新/解决方案
非常感谢并帮助我解决我的问题。您可以查看我所做的更改,这是因为test.ros中的第2行是空的 在本例中,您将创建以下内容:
['! This is a single line comment',
'',
'!!!',
'This line should be ignored',
'and this one as well',
'!!!',
'',
'print_message.Hello World']
当您尝试访问内容[1][0]时,会得到一个索引器,因为它是一个空字符串
尝试通过向列表中添加if来删除内容中的空行:
content = [x.strip() for x in content if x.strip()]
您应该立即解压缩enumerate的返回元组:对于索引,enumeratecontent中的值:。然后检查值是否以开始!喜欢值。从“!”开始。这将防止空值上的索引错误。谢谢您的建议。我已经把你的建议和Idanmel的建议结合起来了,现在效果很好,谢谢!我已将代码行添加到相应的位置。是的!它确实奏效了。我已经发布了一个概述我所做更改的答案。@RichieBendall不要发布您修复的代码。如果你找到一个有用的答案,接受它:好的,我已经接受了你的帖子作为答案。谢谢你指出这一点!