[Tutor] Text Processing - Questions/Help.

Alan Gauld alan.gauld at btinternet.com
Thu Mar 14 13:14:11 CET 2013


On 14/03/13 10:30, David Bradshaw wrote:
> I have written some code that logs into a list of hosts and runs a
> couple of commands to find out some information. Which I then save to a
> file and process.
>
> A cut down version of the information I generate and save can be found
> here - http://pastebin.com/4ePz3Z7m
>
> The code I have written to process this file can be found here -
> http://pastebin.com/WBbKydLg

Its fine to use pastebin for long programs.
But...

> information to generate a dhcpd.conf file. I have written the code to do
> it, but it suffers the same problem.

It's not totally clear what the problem is?

> Currently when I run my code I get this as the output -
>
>       BBC21-14.ZONE21.WORK
>       178.28.185.89
> 6     vm14-6
>       06BNBB6
>       5C:F3:FC:C2:08:0C
> 7     vm14-7
>       06BNBM3
>       5C:F3:FC:C2:0D:48
> 8     s21-04
>       06KKY20
>       5C:F3:FC:79:53:50
>   00:10:18:E4:EA:EC
> 9     ps41.p33
>       06CZHR8
>       34:40:B5:DC:2C:50
>       00:10:18:E4:F3:2C
> 10     vm14-8
>       06BNBN6
>       5C:F3:FC:C1:F3:10
>
> Any pointers as to how in the cases where 2 MACs are displayed I only
> display/output the second? Also there is no guarantee that in the future
> the second MAC will start with 00:10.

See the other thread about text processing. You need top use a flag, or 
sentinel. In your case the flag tells you when you stop getting MAC 
addresses, so in pseudo code

inMacs = False
for line in inputfile:
    if not inMacs and isMac(line):  # your function to identify a MAC
       inMacs = True
    if inMacs:
        if isMac(line):
           lastMac = line
        else:
            print lastMac
            inMacs = False


> From my code was it a fluke that I got the output formatted as it is
 > and I actually need to rethink how I process the information?

Probably not, it'll be because that's how you processed it. But without 
reading your code I can't tell what that was. Whether the formatting is 
due to an explicit decision on your part or a result of some default 
Python behaviour I don't know, but it almost certainly won't be a fluke.
(The exception is if its down to the order/structure of your input data)

> Finally the information on pastebin, is it better or preferred that I
> add it all in this thread, rather the putting it in pastebin?

Its best if you can either post the relevant snippet from your code 
(with optional link to the full listing) or recreate the situation using 
a small example program.

Having nothing in the mail puts too much onus on the reader to do all 
the work. If they are busy, lazy, or both, they won't bother.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list