[Tutor] print issue

Alan Gauld alan.gauld at btinternet.com
Tue Oct 1 04:05:53 EDT 2019


On 01/10/2019 04:18, ose micah via Tutor wrote:
> here is my the main block of code.
> f = open("output.txt").read().count('\n')
You do a lot of work to find the number of newlines but never use it?
> sys.stdout=open('samplefile.txt','a')
> with open('output.txt', 'r') as reader:
>
>     for line in reader.readlines():

You don't need the readlines() just use


for line in reader:

>         #from __future__ import print_function

The future line should really be at the top of your code not inside the
loop.

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

You already opened this file. Its a really bad idea to have the
same file opened for output multiple times. You are very likely to
overwrite data. As it is you never seem to use this p reference...

Personally I'd suggest you don;t use the stdout version but
write to p rather than use print statements. But that's really
just a style and convenience thing - you can keep print for
displaying to the console for debugging etc.

>             # 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()
Note that at this point you no longer have a valid stdout so
you can no longer print anything. That's a risky position
in which to put yourself
> 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]
The above is presumably what wasalready in samplefile.txt before you
started appending your data.
> ("           - [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]')
The above is the data you printed - note the parens are because you are
still using Python 2 style prints because the import future didn't work. 
Python sees your print as being a tuple of strings so that's what it prints.
>
> 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]
>
That's almost exactly what you told it to print(with an extra coma?).
I'm not sure what you expected? I'm not sure what you mean by "scattered"?

You don't get parens this time because the string addition means Python
sees a single string inside the parens so does not treat it as a tuple.

> Please, how can I get my desired result.

I'm not clear what your desired result is but I would:


1) Put the import future at the top of the code to make print work

(or better still just use Python v3. Python v2 loses support next year
so you are playing with fire by sticking with it)

2) Stop using print and sys.stdout as your output file and convert
from using print to p.write() instead.(Just remember that you need
to add separators and newlines when using write())

3) Use format strings to control the layout and positioning of
your output, it is more consistent than counting spaces etc

-- 

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