2013-06-29 4 views
0

파일을 열고 행의 모든 ​​유효하지 않은 ID 목록을 가져 오려고합니다. 잘못된 ids..on을 찾았습니다 .i 하나의 ID가 유효하지 않은 경우 전체 행 (여러 개의 ID가 있음)을 삭제하려고합니다. 잘못된 ID를 찾는 지점에 도달 할 수 있습니다. 파일에서 행을 삭제하는 방법에 대한 입력이 필요하거나 잔잔한 행을 작성하십시오 새 파일에 .. 내가 샘플 입력 및 예상 출력을 아래에 ... 아무도 입력을 제공 할 수 있습니까?행의 id 중 하나가 유효하지 않은 경우 파일에서 행 삭제

import os 
import sys 
import time 
import simplejson as json 
from sets import Set 
import operator 
import unicodedata 
import getopt 

''' 
list.txt 

350882 348521 350166 
346917 352470 
360049 
''' 

''' 
EXPECTED OUTPUT:- 
346917 352470 
360049 
''' 
def func (GerritId): 
    if GerritId == '350166': 
     value = "GERRIT IS INCOMPLETE" 
    else: 
     value = "GERRIT LOOKS GOOD" 
    return value 

gerrit_list=[] 
invalid_gerrit = [] 
cherry_pick_list = [' '] 
with open('list.txt','r') as f : 
    for line in f : 
     gerrit_list = line.split(' ') 
     print "line" 
     print line 
     print "gerrit_list" 
     print gerrit_list 
     for GerritId in gerrit_list : 
      GerritId = GerritId.strip() 
      print "GerritId" 
      print GerritId 
      #returnVal = RunCheckOnGerrit_Module.GerritCheck(GerritId) 
      #GerritInfoItem['GerritId'] = GerritInfoItem['GerritId'] + "\n" 
      returnVal = func(GerritId) 
      #print "returnVal" 
      #print returnVal 
      if returnVal in ('GERRIT IS INCOMPLETE' or 'NOTHING IS SET' or 'TO BE ABANDON OR NEEDS RESUBMISSION') : 
       print returnVal 
       invalid_gerrit.append(GerritId) 
      else: 
       print returnVal 

print invalid_gerrit 

with open('list.txt','r') as f : 
    for line in f : 
     #delete the whole line if any invalid gerrit is presnet 
     gerrit_list = line.split(' ') 
     print "line" 
     print line 
     print "gerrit_list" 
     print gerrit_list 
     for GerritId in invalid_gerrit: 
      GerritId = GerritId.strip() 
      #delete the whole line if any invalid gerrit is presnet 
+0

http://stackoverflow.com/questions/4710067/deleting-a-specific-line -in-a-file-python은 필요한 것을 제공합니다. – ojs

+0

@ojs - 줄을 지우는 데 도움이 될 것입니다 .i 내 파일의 줄이 ids 인 문제가 하나 더 있습니다 ... anyof ID가 유효하지 않으면 줄을 삭제해야합니다. – user2341103

+0

그런 다음 줄을 'if ! = "nickname_to_delete"+ "\ n":'id가 줄에 있는지 테스트합니다. 라인을 목록으로 읽고 잘못된 ID가 목록에 있는지 확인하십시오. – ojs

답변

0

이 새 파일 만 유효한 ID를 가진 라인을 기입하는 원유 코드 :

f_write = open('results.txt', 'wb') 

with open('list.txt','r') as f : 
    for line in f : 
     #delete the whole line if any invalid gerrit is presnet 
     gerrit_list = line.strip().split(' ') 

     ifvalid = True 
     for gerrit in gerrit_list: 
      try: # check if invalid gerrit is present 
       invalid_gerrit.index(gerrit) 
       ifvalid = False 
       break 
      except: 
       pass 

     if ifvalid: 
      f_write.write(line) 

f_write.close() 
관련 문제