[Tutor] print issue
ose micah
osaosemwe at yahoo.com
Wed Oct 2 11:30:35 EDT 2019
Thank you for the suggestion. I hope the format below is OK?
Please have a look at this issue, I need solving.
>From the opened file "output.txt" that contains subnets each of which I need in creating a rule, I wrote a program that read each line from this file and make use of each subnet to append a yaml formatted line in the samplefile.txt.
Here is my desired output in the "samplefile.txt":
- [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]
- [500, 'sing', 'play', 172.50.10.34/24, null, null, 500, 500]
- [600, 'sing', 'play', 172.50.10.34/24, null, null, 800, 800]
- [700, 'sing', 'play', 192.168.230.10/24, null, null, 500, 500]
- [800, 'sing', 'play', 192.168.230.10/24, null, null, 800, 800]
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:
f = open("output.txt").read().count('\n') # file that contains subnets
sys.stdout=open('samplefile.txt','a') . #where I want the configured subnets dumped
with open('output.txt', 'r') as reader:
for line in reader.readlines():
with open ('samplefile.txt', 'a') as p:
# allow ip to play port 500
print(" - [100, 'tcp', 'allow', ", line, ", null, null, 500, 500]")
# allow ip to sing port 800
print(" - [200, 'tcp', 'allow', ", line, " , null, null, 800, 800]")
Here is my current output in the "samplefile.txt"- Which is undesired
(" - [100, 'tcp', 'allow', ", '10.10.10.10/24\n', ', null, null, 500, 500]') (" - [200, 'tcp', 'sing', ", '10.10.10.10/24\n', ', null, null, 800, 800]') (" - [100, 'tcp', 'allow', ", ' 10.10.20.10/24\n', ' , null, null, 500, 500]') (" - [200, 'tcp', 'allow', ", ' 10.10.20.10/24\n', ', null, null, 800, 800]') (" - [100, 'tcp', 'allow', ", ' 172.50.10.34/24\n', ' , null, null, 500, 500]') (" - [200, 'tcp', 'allow', ", ' 172.50.10.34/24\n', ', null, null, 800, 800]') (" - [100, 'tcp', 'allow', ", ' 192.168.230.10/24\n', ' , null, null, 500, 500]') (" - [200, 'tcp', 'allow', ", ' 192.168.230.10/24\n', ', null, null, 800, 800]')
Please how can I change my code to get my desired output as: - [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]
- [500, 'sing', 'play', 172.50.10.34/24, null, null, 500, 500]
- [600, 'sing', 'play', 172.50.10.34/24, null, null, 800, 800]
- [700, 'sing', 'play', 192.168.230.10/24, null, null, 500, 500]
- [800, 'sing', 'play', 192.168.230.10/24, null, null, 800, 800]
Thanks
Mike.
On Tuesday, October 1, 2019, 12:50:32 PM EDT, Alan Gauld <alan.gauld at btinternet.com> wrote:
> Thank you for the suggestion. I think its best I attach the code
The Python Tutor mailing list, in common with most technical lists,
is a text only service. As such non text attachments are usually stripped
out by the server for security reasons. You are best either posting code
inline within the message (if less than, say 100 lines) or on a pastebin
and sending a link.
--
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