停止时的python txt文件

2024-10-04 11:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在想如何根据情况停下来。停止$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.

Tags: testtxtsourceinputrawif节点def
2条回答

谢谢,这段代码很管用,:)

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

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

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

if condition1:
    reaction1
elif condition2:
    reaction2

或者

if condition1:
    reaction1
else:
    if condition2:
        reaction2

或者,如果您知道condition1condition2不能同时发生,那么这会做同样的事情:

if condition1:
    reaction1
if condition2:
    reaction2

缩进的程度很重要。你知道吗

相关问题 更多 >