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

Recommended Answers

All 2 Replies

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)

Thanks a lot Jice. I get your answer just when I need it.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.