小编New*_*bie的帖子

Python for循环中的重置行

我试图反复浏览几行文本文件.当我的for循环完成一遍文件后,我想从头开始重复该文件.有没有办法重置线计数器来实现这一目标?

infile=open("some_txt.txt","r")

choice=input("Choice:")
g=0

while choice!="close":
    if choice=='1':

        take_off=input("Take off:")
        for line in infile:
            x=line.split()
            if x[1]==take_off:
                print(x)
                g=g+1

        if g==0:
            print("No match.")

    elif choice=='2':

        take_off=input("Take off 2:")
        for line in infile:
            x=line.split()
            if x[2]==take_off:
                print(x)
                g=g+1

        if g==0:
            print("No match.")

    choice=input("Choice")
Run Code Online (Sandbox Code Playgroud)

python for-loop line reset

0
推荐指数
1
解决办法
3243
查看次数

标签 统计

for-loop ×1

line ×1

python ×1

reset ×1