Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止时的python txt文件_Python_While Loop - Fatal编程技术网

停止时的python txt文件

停止时的python txt文件,python,while-loop,Python,While Loop,我正在寻找如何根据情况停止一段时间。停止$config1工作。 如何进入第一站!在节点$config1之后 def filtre(source): fs = open(source, 'r') while 1: txt = fs.readline() if txt =='': break else: print txt if txt == "$ config1\n": raw_input("====

我正在寻找如何根据情况停止一段时间。停止$config1工作。 如何进入第一站!在节点$config1之后

def filtre(source):
fs = open(source, 'r')
while 1:
    txt = fs.readline()
    if txt =='':
        break

    else:
        print txt
        if txt == "$ config1\n":
            raw_input("==========================>")

            if txt == "!\n":
               raw_input("==========================>") 

fs.close()
return
txt文件

! ************************************
! *  Export File:  X:\test.txt
! *      Created by:  PC2
! *   Creation Date:  10/01/2014 18:50
! *            User:  test
! *************************************
!
!
$ config1
MCBR:54:62:2:32:.5:x34:y637::::
MCBR:54:62:2:32:.5:x34:y637::::
MCBR:54:62:2:32:.5:x34:y637::::
MCBR:54:62:2:32:.5:x34:y637::::
!
!
$ End of file.

您将一个
if
语句嵌套在另一个语句中,我认为您不打算这样做

Python中的“if,else if”可以写成:

if condition1:
    reaction1
elif condition2:
    reaction2

或者,如果您知道
condition1
condition2
不能同时发生,则执行相同的操作:

if condition1:
    reaction1
if condition2:
    reaction2

缩进的级别很重要。

谢谢,这段代码很有效,:)


你到底想干什么?你的问题不是很清楚你为什么在文件对象上使用
while
循环而不是
for
循环<代码>for line in file是迭代文件中的行的常用方法,它比执行大量的
readline
调用简单得多。您的代码在“$config1\n”和“!\n”两行中都已停止。问题是什么?
def filter(source):fs=open(source,'r')started=false,而1:txt=fs.readline()如果txt='':break-else:print txt如果txt==“$config1\n”:started=true原始输入(“============”)如果txt==“!\n”:break fs.close()返回值
if condition1:
    reaction1
if condition2:
    reaction2
def filtre(source):
fs = open(source, 'r')
started = false
while 1:
    txt = fs.readline()
    if txt =='':
        break

    else:
        print txt
        if txt == "$ config1\n":
            started = true
            raw_input("==========================>")

            if started and txt == "!\n":
               break 

fs.close()
return