0

I am trying to open a file and get the list of all the invalid ids from the lines in it..once I find the invalid ids..i want to delete the whole line(it has multiple ids) if if one id is invalid...i could reach to point of finding invalid ids..i need inputs on how to delete the line from the file or write the remaning good lines into a new file..i have the sample input and expected output below...can anyone provide inputs?

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
user2341103
  • 2,333
  • 5
  • 20
  • 19
  • http://stackoverflow.com/questions/4710067/deleting-a-specific-line-in-a-file-python should give you what you need. – ojs Jun 29 '13 at 01:09
  • @ojs - that will help to delete a line..i have one more problem where the lines in my file are ids...i need the delete the line if anyof the id is invalid – user2341103 Jun 29 '13 at 01:12
  • Then, instead of the `if line!="nickname_to_delete"+"\n":` test for if the id is in the line. Read the line in as a list and check if the invalid id is in the list. – ojs Jun 29 '13 at 01:19

1 Answers1

0

This is a crude code which will write lines with only valid ids to a new file:

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()
joon
  • 3,899
  • 1
  • 40
  • 53