Python 而循环字符串索引超出范围?

Python 而循环字符串索引超出范围?,python,while-loop,Python,While Loop,我使用以下语法在Python中运行while循环: while not endFound: if file[fileIndex] == ';': current = current + ';' contents.append(current) if fileIndex == lengthOfFile: endFound = True else: current = current + file

我使用以下语法在Python中运行
while
循环:

while not endFound:
    if file[fileIndex] == ';':
        current = current + ';'
        contents.append(current)
        if fileIndex == lengthOfFile:
            endFound = True
    else:
        current = current + file[fileIndex]
    fileIndex = fileIndex + 1
我在我的控制台中发现了这个错误:

var(vari) = 0;terminal.write(vari);var(contents) = file.getContents('source.py');if(vari : 0) {    terminal.write('vari equals 0');}
Traceback (most recent call last):
  File "/home/ubuntu/workspace/source.py", line 30, in <module>
    splitFile(content)
  File "/home/ubuntu/workspace/source.py", line 22, in splitFile
    if file[fileIndex] == ';':
IndexError: string index out of range

> Process exited with code: 1
var(vari)=0;终端写入(vari);var(contents)=file.getContents('source.py');if(vari:0){terminal.write('vari=0');}
回溯(最近一次呼叫最后一次):
文件“/home/ubuntu/workspace/source.py”,第30行,在
拆分文件(内容)
文件“/home/ubuntu/workspace/source.py”,第22行,拆分文件
如果文件[fileIndex]==';':
索引器错误:字符串索引超出范围
>进程已退出,代码为:1
发生了什么事

  • 进入循环之前,
    fileIndex
    的值是多少

  • 如果文件[fileIndex]==';',则检查字符串结尾(
    如果fileIndex==lengthOfFile:
    )是否在
    的内部:,所以如果没有
  • 对字符的操作不是很像python,可能有更有效的工具来做你想做的事情(比如
    .index
    方法
    str
    等等)


  • 在开始之前,我假设你有类似的东西:

    file = "section_1;section_2;section_3;"
    lengthOfFile = len(file)
    contents = []
    current = ""
    fileIndex = 0
    endFound = False
    
    您编写的代码可以稍微澄清如下:

    while not endFound:
        next_char = file[fileIndex]
        current = current + next_char
        if next_char == ';':
            contents.append(current)
            #? current = ''
            if fileIndex == lengthOfFile:
                endFound = True
        fileIndex = fileIndex + 1
    
    这种特殊情况下的问题是,当您到达最终的
    文件
    中,
    文件索引
    为17,但
    长度办公室
    为18。因此,
    fileIndex==lengthOfFile
    测试失败。您可以通过将此行更改为
    fileIndex+1==lengthOfFile
    ,或者如果next_char==';,则将增量操作移到
    上方,来修复上面的代码

    但是,用Python编写此代码有更简单的方法。特别是,如果您的目标是使
    内容
    成为
    文件
    中所有“section\n;”条目的列表,那么您可以使用如下内容:

    contents = [part + ';' for part in file[:-1].split(';')]
    
    contents1 = file[:-1].split(';')
    contents = []
    for part in contents1:
        current = current + part + ';'
        contents.append(current)
    
    (拆分前,
    [:-1]
    会省略
    文件中的最后一个字符(
    )。请注意,如果这是您想要的,则原始代码还需要在每次传递过程中重置
    当前
    的值,如上所述

    如果您确实希望
    内容
    文件
    的开头变成一个越来越长的子字符串列表(如当前编写的),您可以执行以下操作:

    contents = [part + ';' for part in file[:-1].split(';')]
    
    contents1 = file[:-1].split(';')
    contents = []
    for part in contents1:
        current = current + part + ';'
        contents.append(current)
    

    事实上,测试了一下,不,它没有:(1.
    fileIndex
    的值从0开始增加,直到等于文件的长度。(此脚本用于在代码中拆分行,以便我更容易阅读,因此分号表示行的结尾。)这是问题吗?无限循环?字符串是这样的:
    “var(vari)=0;terminal.write(vari);var(contents)=file.getContents('source.py');if(vari:0){terminal.write('vari=0');};“
    不过有一个分号结尾。