2012-07-25 82 views
2

我有串的文件行:如何打印只包含一個子

REALSTEP 12342 {2012-7-20 15:10:39};[{416 369 338};{423 432 349};{383 380 357};{399 401 242};{0 454 285};{457 433 115};{419 455 314};{495 534 498};][{428 377 336};{433 456 345};{386 380 363};{384 411 
REALSTEP 7191 {2012-7-20 15:10:41};[{416 370 361};{406 417 376};{377 368 359};{431 387 251};{0 461 366};{438 409 134};{429 411 349};{424 505 364};][{423 372 353};{420 433 374};{379 365 356};{431 387 2 
REALSTEP 12123 {2012-7-20 15:10:42};[{375 382 329};{386 402 347};{374 378 357};{382 384 259};{0 397 357};{442 424 188};{398 384 356};{392 420 355};][{404 405 359};{420 432 372};{405 408 383};{413 407 
REALSTEP 27237 {2012-7-20 15:10:44};[{431 375 329};{416 453 334};{387 382 349};{397 403 248};{0 451 300};{453 422 131};{433 401 317};{434 505 326};][{443 384 328};{427 467 336};{391 386 344};{394 413 
FAKE 32290 {2012-7-20 15:10:48};[{424 399 364};{408 446 366};{397 394 389};{415 409 261};{0 430 374};{445 428 162};{432 416 375};{431 473 380};][{424 398 376};{412 436 372};{401 400 390};{417 409 261} 
FAKE 32296 {2012-7-20 15:10:53};[{409 326 394};{445 425 353};{401 402 357};{390 424 250};{0 420 353};{447 423 143};{404 436 351};{421 527 420};][{410 332 400};{450 429 356};{402 403 356};{391 425 250} 
FAKE 32296 {2012-7-20 15:10:59};[{381 312 387};{413 405 328};{320 387 376};{388 387 262};{0 402 326};{417 418 177};{407 409 335};{443 502 413};][{412 336 417};{446 437 353};{343 417 403};{417 418 258} 
FAKE 32295 {2012-7-20 15:11:4};[{377 314 392};{416 403 329};{322 388 375};{385 391 261};{0 403 329};{425 420 168};{414 393 330};{458 502 397};][{408 338 421};{449 435 355};{345 418 403};{413 420 257}; 
FAKE 32295 {2012-7-20 15:11:9};[{371 318 411};{422 385 333};{342 379 352};{394 395 258};{0 440 338};{418 414 158};{420 445 346};{442 516 439};][{401 342 441};{456 415 358};{367 407 377};{420 420 255}; 
FAKE 32296 {2012-7-20 15:11:15};[{373 319 412};{423 386 333};{344 384 358};{402 402 257};{0 447 342};{423 416 151};{422 450 348};{447 520 442};][{403 342 442};{456 416 358};{366 409 379};{422 421 255} 
REALSTEP 7191 {2012-7-20 15:10:41};[{416 370 361};{406 417 376};{377 368 359};{431 387 251};{0 461 366};{438 409 134};{429 411 349};{424 505 364};][{423 372 353};{420 433 374};{379 365 356};{431 387 2 
REALSTEP 12123 {2012-7-20 15:10:42};[{375 382 329};{386 402 347};{374 378 357};{382 384 259};{0 397 357};{442 424 188};{398 384 356};{392 420 355};][{404 405 359};{420 432 372};{405 408 383};{413 407 
REALSTEP 27237 {2012-7-20 15:10:44};[{431 375 329};{416 453 334};{387 382 349};{397 403 248};{0 451 300};{453 422 131};{433 401 317};{434 505 326};][{443 384 328};{427 467 336};{391 386 344};{394 413 

我讀與readlines方法)的文件(並希望然後遍歷所有的線條和打印時,纔會有一個連續大於3的行塊,包含字符串「REALSTEP」。因此在本例中預期的結果是:

REALSTEP 12342 {2012-7-20 15:10:39};[{416 369 338};{423 432 349};{383 380 357};{399 401 242};{0 454 285};{457 433 115};{419 455 314};{495 534 498};][{428 377 336};{433 456 345};{386 380 363};{384 411 
REALSTEP 7191 {2012-7-20 15:10:41};[{416 370 361};{406 417 376};{377 368 359};{431 387 251};{0 461 366};{438 409 134};{429 411 349};{424 505 364};][{423 372 353};{420 433 374};{379 365 356};{431 387 2 
REALSTEP 12123 {2012-7-20 15:10:42};[{375 382 329};{386 402 347};{374 378 357};{382 384 259};{0 397 357};{442 424 188};{398 384 356};{392 420 355};][{404 405 359};{420 432 372};{405 408 383};{413 407 
REALSTEP 27237 {2012-7-20 15:10:44};[{431 375 329};{416 453 334};{387 382 349};{397 403 248};{0 451 300};{453 422 131};{433 401 317};{434 505 326};][{443 384 328};{427 467 336};{391 386 344};{394 413 

我嘗試這樣做:

lines = f.readlines() 
idx = -1 
#loop trough all lines in the file 
for i, line in enumerate(lines): 
    if idx > i: 
     continue 
    else: 
     if "REALSTEP" in line: 
      steps = lines[i:i+3] 
      #check for blokc of realsteps 
      if is_block(steps, "REALSTEP") == 3: 
       #prints the block up to the first next "FAKE STEP" 
       lst = get_block(lines[i:-1]) 
       for l in lst: 
        print l[:200] 
       idx = i + len(lst) 
       print "next block============" 

其中函數is_block是這樣的:

def is_block(lines, check): 
    #print len(lines) 
    bool = 1 
    for l in lines: 
     if check in l: 
      bool = 1 
     else: 
      bool = 0 
     bool = bool + bool 
    return bool 

和功能get_block:

def get_block(lines): 
    lst = [] 
    for l in lines: 
     if "REALSTEP" in l: 
      #print l[:200] 
      lst.append(l) 
     else: 
      break 
    return lst 

雖然這個工程,它會打印包含字符串「REALSTEPS」的所有行。當函數被調用時,is_block(行)中的print len(行)始終爲10,所以不是這樣。

我很困惑,請幫助我在這裏!

回答

5

下面是一個包含你所需要的邏輯簡單的解決方案:

to_print = [] 
count = 0 
started = False 
for line in f.readlines(): 
    if "REALSTEP" in line: 
     if not started: 
      started = True 
     to_print.append(line) 
     count += 1 
    else: 
     if count > 3: print('\n'.join(to_print)) 
     started = False 
     count = 0 
     to_print = [] 

,計算出在它的字符串「REALSTEP」任何有效的線。產生所需的輸出。

+0

+1清晰簡潔。 – 2012-07-25 19:43:01

+0

除了'如果沒有開始:開始=真實'... – 2012-07-25 19:43:34

+0

我通常不認爲交出銀盤上的答案是最好的方式來做到這一點...沒有人學習。但是,哦。 – voithos 2012-07-25 19:44:27

0

這一部分:

... 
    if "REALSTEP" in line: 
     steps = lines[i:i+3] 
     for s in steps: 
      print s[:200] # <- right here 
... 

每當你在一條線上找到"REALSTEP",您檢索以下三行,並打印出來的時候了。這可能不是你想要的。

+0

確實,那不是我想要的。我刪除了它,但問題仍然存在。它只在那裏進行測試。我會再看看明天 – jorrebor 2012-07-25 20:11:03