小编All*_*ell的帖子

混合文件和循环

我正在编写一个脚本来记录来自另一个程序的错误,并在遇到错误时重新启动它停止的程序.无论出于何种原因,该程序的开发人员都认为没有必要在默认情况下将此功能放入其程序中.

无论如何,程序接受一个输入文件,解析它,并创建一个输出文件.输入文件采用特定格式:

UI - 26474845
TI - the title (can be any number of lines)
AB - the abstract (can also be any number of lines)
Run Code Online (Sandbox Code Playgroud)

当程序抛出错误时,它会为您提供跟踪错误所需的参考信息 - 即UI,哪个部分(标题或摘要)以及相对于标题或摘要开头的行号.我想从输入文件中记录有问题的句子,其中包含一个带参考号和文件的函数,找到句子并记录它.我能想到的最好的方法是在文件中向前移动特定次数(即n次,其中n是相对于seciton开头的行号).这样做的方式似乎是:

i = 1
while i <= lineNumber:
    print original.readline()
    i += 1
Run Code Online (Sandbox Code Playgroud)

我不知道这会如何让我丢失数据,但Python认为它会,并说ValueError: Mixing iteration and read methods would lose data.有谁知道如何正确地做到这一点?

python loops file while-loop python-2.7

27
推荐指数
2
解决办法
4万
查看次数

循环条件评估时的Python

说我有以下循环:

i = 0
l = [0, 1, 2, 3]
while i < len(l):
    if something_happens:
         l.append(something)
    i += 1
Run Code Online (Sandbox Code Playgroud)

是否会在len(i)附加内容时更新while循环中评估的条件l

python while-loop

2
推荐指数
1
解决办法
4068
查看次数

标签 统计

python ×2

while-loop ×2

file ×1

loops ×1

python-2.7 ×1