-4

I am giving a text file with the format below:

3     Bham    Hoover - Vestiva 
123 234 1 456 876 1 876 745 1
0
4     Bham    Vestiva - Greensprings
235 876 1 647 987 1 098 765 1 234 546 1
0

This goes on for several more lines, but I am trying to convert this format to the following:

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event 
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end
Levon
  • 138,105
  • 33
  • 200
  • 191
user1431707
  • 11
  • 1
  • 2

3 Answers3

1
from itertools import islice
with open('file.txt', 'r') as f:
    # iterate over every 3rd line, starting with the 2nd
    for line in islice(f, 1, None, 3):
        parts = line.split()
        print 'Event'
        # iterate over 3-element chunks
        for x in zip(*(iter(parts),) * 3):
            print 'Disconnect branch from bus %s to %s circuit %s' % x
        print 'end'

Output:

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end

Credits to the chunking code go to Iteration over list slices

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0
with open("data.txt") as inf:
    for head,data,tail in zip(inf,inf,inf):
        print("Event")
        items = iter(data.split())
        for bus1,bus2,cct in zip(items,items,items):
            print("Disconnect branch from bus {} to {} circuit {}".format(bus1,bus2,cct))
        print("end")

results in

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
-1

I'm not certain about your real necessity. But if you want to output your file like you showed, you can insert a delimiter on your text and use the re module to parse then and get the data in ther order that you need !

StarkBR
  • 227
  • 8
  • 15
  • Regexes are not really a good choice for this format since the data line contains an arbitrary number of data triples. There are better way to parse this - see hugh's answer or mine. – ThiefMaster Jun 01 '12 at 23:31
  • Definitely see ThiefMaster's answer, particularily using a file object as a generator, and skippinng lines with `islice` (I learn something new everyday on here). – yurisich Jun 02 '12 at 01:16