[Tutor] print issue

Alan Gauld alan.gauld at btinternet.com
Wed Oct 2 12:38:58 EDT 2019


On 02/10/2019 16:30, ose micah wrote:
>
>
> Thank you for the suggestion. I hope the format below is OK?


Seems Ok to me.

>  
>                 - [100, 'sing', 'play', 10.10.10.10/24,  null, null,
> 500, 500]
>                 - [200, 'sing', 'play', 10.10.10.10/24,  null, null,
> 800, 800]
>                 - [300, 'sing', 'play', 10.10.20.10/24,  null, null,
> 500, 500]
>                 - [400, 'sing', 'play', 10.10.20.10/24,  null, null,
> 800, 800]


Notice you increment the first entry here but in your code you always
use 100/200.

You need to create a variable and increment it by 100 each time.

           
>
> I pick each subnets from "output.txt" is:
>   
>       10.10.10.10/24
>       10.10.20.12/24
>       172.50.10.34/24
>       192.168.230.10/24
>
> Here is my current line of code which need modification:
>       

We have already given you several suggestions, I will incorporate them
below: I will assume that for some reason you need to use Python v2...
with open('output.txt', 'r') as reader: counter = 100 fmtString = "%11s-
[%d, 'tcp', 'allow', %s, null, null, %d, %d]\n" for line in reader:
address = line.strip() with open ('samplefile.txt', 'w') as p:
p.write(fmtString % (" ", counter, address, 500,500)) counter +=100
p.write(fmtString % (" ", counter, address, 800,800)) counter += 100
Note that I changed the file mode to 'w' to create a new file each time
rather than keep appending since it seems likely that is what you really
want. If I'm mistaken change it back to 'a'... The code is untested so
may need tweaking but should be close.

-- 

Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list