[Tutor] Text Processing - Questions/Help.

David Bradshaw dave_bradshaw at talk21.com
Mon Mar 18 10:37:57 CET 2013


Alan, Thanks.

See below [Dave]


________________________________
 From: Alan Gauld <alan.gauld at btinternet.com>
To: tutor at python.org 
Sent: Thursday, 14 March 2013, 12:14
Subject: Re: [Tutor] Text Processing - Questions/Help.
 
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

[Dave] - Thanks, I will give this a go and see if it works, and report back.


> 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.

[Dave] - Will remember this for the next time I post.

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

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130318/d7181b2a/attachment-0001.html>


More information about the Tutor mailing list