0

Hi guys,
I got a file having this kind of records:

000000000111GOOGLE 5428MG45525RC
000000000122MICROSOFT 8565MG85454RE
000000000044APPLE 5814KL484545RC

I want to keep only the records that have 'MG' in position 34 and NOT ('RE' or 'RD') in position 41
What should I add to this code?

#!/usr/bin/python
infile = 'DATA'
input = open(infile, 'r').readlines()
outfile = 'OUTPUT'
output = open(outfile, 'w')

for line in input:
linelist = line.split()
if ???? :
output.write(line)
break

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by thoutmosis
0
outfile = 'OUTPUT'
output = open(outfile, 'w')

for line in open('DATA'): # You don't need to open the file and load it in a list (bad for memory)
    linelist = line.strip().split() # Strip will remove trailing spaces and '\n'
    if linelist[1][4:6]=='MG' and linelist[1][-2:] not in ['RE', 'RD']:
        output.write(line)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.