[Tutor] Print issue please help

ose micah osaosemwe at yahoo.com
Tue Oct 1 03:38:39 EDT 2019


Hello please I am trying to make a dynamic print format 
Assuming "output.txt" has contents as      10.10.10.10/24      10.10.20.12/24      172.50.10.34/24      192.168.230.10/24and "samplefile.txt"  is an empty file
I am trying to get an output such as in file sample.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]  here is my the main block of code.
f = open("output.txt").read().count('\n')
sys.stdout=open('samplefile.txt','a')
with open('output.txt', 'r') as reader:

    for line in reader.readlines():

        #from __future__ import print_function

        with open ('samplefile.txt', 'a') as p:

            # allow ingress port 80

            print("                          - [100, 'tcp', 'allow', ",line, " , null, null, 500, 500]")

            print("                          - [200, 'tcp', 'allow', ",line,"  , null, null, 800, 800]")





sys.stdout.close()

But here is my current output in 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]



("           - [100, 'tcp', 'allow', ", '10.10.10.10/24\n', ', null, null, 500, 500]')

("           - [200, 'tcp', 'allow', ", '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]')

when I adjust change the print statement to:
print("                          - [100, 'tcp', 'allow', " + line + " , null, null, 500, 500]")
print("                         - [200, 'tcp', 'allow', " + line  +" , null, null, 800, 800]")


the print get scattered. something like this:
          - [100, 'tcp', 'allow', '10.10.10.10/24\n', ', null, null, 500, 500]
         - [200, 'tcp', 'allow', '10.10.10.10/24\n', ', null, null, 800, 800]


Please, how can I get my desired result.
Can anyone be of help to me. 
Thanks

Mike


More information about the Tutor mailing list