user1650583
user1650583

Reputation: 23

Python: re-formatting multiple lines in text file

I apologize if this post is long, but I am trying to be as detailed as possible. I have done a considerable amount of research on the topic, and would consider myself an "intermediate" skilled programmer.

My problem: I have a text file with multiple lines of data. I would like to remove certain parts of each line in an effort to get rid of some irrelevant information, and then save the file with the newly formatted lines.

Here is an example of what I am trying to accomplish. The original line is something like:

access-list inbound_outside1 line 165 extended permit tcp any host 209.143.156.200 eq www (hitcnt=10086645) 0x3eb90594

I am trying to have the code read the text file, and output:

permit tcp any 209.143.156.200 www 

The following code works, but only if there is a single line in the text file:

input_file = open("ConfigInput.txt", "r")
output_file = open("ConfigOutput.txt", "w")

for line in input_file:
    line = line.split("extended ", 1)[1]
    line = line.split("(", 1)[0]
    line = line.replace(" host", "")
    line = line.replace(" eq", "")
    output_file.write(line)

output_file.close()
input_file.close()

However, when I attempt to run this with a full file of multiple lines of data, I receive an error:

File "C:\Python27\asaReader", line 5, in <module>
    line = line.split("extended ", 1)[1]
IndexError: list index out of range

I suspect that it is not moving onto the next line of data in the text file, and therefore there isn't anything in [1] of the previous string. I would appreciate any help I can get on this.

Upvotes: 2

Views: 915

Answers (3)

Rg Glpj
Rg Glpj

Reputation: 443

An alternate approach would be to cache all the lines (assuming the file is not humongous.)

>>> with open('/tmp/ConfigInput.txt', 'rU') as f:
...     lines = f.readlines()
...     
... 
>>> lines
['access-list inbound_outside1 line 165 extended permit tcp any host 209.143.156.200 eq www (hitcnt=10086645) 0x3eb90594\n']
>>> lines = [re.sub('(^.*extended |\(.*$)', '', line) for line in lines]
>>> lines
['permit tcp any host 209.143.156.200 eq www \n']
>>> with open('/tmp/ConfigOutput.txt', 'w') as f:
...     f.writelines(lines)
...     
... 
>>> 

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54242

Some possible causes:

  • You have blank lines in your file (blank lines obviously won't contain the word extended)
  • You have lines that aren't blank, but don't contain the word extended

You could try printing your lines individually to see where the problem occurs:

for line in input_file:
    print("Got line: %s" % (line))
    line = line.split("extended ", 1)[1]

Oh, and it's possible that the last line is blank and it's failing on that. It would be easy enough to miss.

Upvotes: 2

John La Rooy
John La Rooy

Reputation: 304205

Print something out when you hit a line that can't be processed

for line in input_file:
    try:
        line = line.split("extended ", 1)[1]
        line = line.split("(", 1)[0]
        line = line.replace(" host", "")
        line = line.replace(" eq", "")
        output_file.write(line)
    except Exception, e:
        print "Choked on this line: %r"%line
        print e

Upvotes: 1

Related Questions