[Tutor] print issue
Cameron Simpson
cs at cskk.id.au
Tue Oct 1 04:03:14 EDT 2019
I will supply some commentry below, inline (the preferred style in this
list). But your problem is hard to understand because your inserted text
looks a bit malformed. At the least you need to leave a blank line
between your prose and your "literal" text (the file contents, the
programme etc).
Also, this is a plain text only list, so any special "rich" formatting
you might be doing will have no value, and may even be the cause of what
I see below.
If your mailer has a "plain text" mode, please use it.
So, to your problem...
On 01Oct2019 03:18, ose micah <osaosemwe at yahoo.com> wrote:
>Hello 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]
You can see how hard this is to read; that's how things appear at my
end.
I'm guessing that output.txt has some subnets, each on their own line
like this:
10.10.10.10/24
10.10.20.12/24
172.50.10.34/24
192.168.230.10/24
I'm guessing your desired output looks like this:
- [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]
so an increasing counter, and 2 lines per subnet with 500 and 800 end
values.
>here is my the main block of code.
>
>f = open("output.txt").read().count('\n')
You seem to be counting the number of lines in the file, but never using
the value "f" afterwards.
>sys.stdout=open('samplefile.txt','a')
This is unusual; it is very uncommon to attach stdout to a different
file. I presume you want to send the result of "print()" to this file,
and print writes to stdout by default. I recommend you open a different
file and use print's "file=" parameter, like this:
with open('samplefile.txt', 'a') as output:
... other code here ...
and use print like this:
print(....., file=output)
and leave stdout alone.
>with open('output.txt', 'r') as reader:
Just a remark: I would open the "append" file _after_ opening the "read"
file. That way, if the read fails (bad filename, bad permissions,
whatever) you never touch the append file at all.
with open('output.txt', 'r') as reader:
with open('samplefile.txt', 'a') as output:
... other code here ...
> for line in reader.readlines():
Text files are themselves iterable, and return lines, so you can write
this:
for line in reader:
but the lines will include the ending newline. So you probably want:
for line in reader:
subnet = line.strip()
and to talk about "subnet" from there on.
> #from __future__ import print_function
Imports from __future__ need to be the first import (and first "code"
line) in your programme; you can't put them in the middle of the code.
This is because they affect how Python treats the entire script.
> 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()
You won't need the .close either. The "with" form of open does the close
for you; much cleaner and safer.
>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]
[...snip...]
I'll presume these are all on separate lines...
>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]
As I remarked above, your lines contain the newline characters from the
original file. Get the subnet string itslf as line.strip() and use
"subnet" instead.
I also think you're printing repr(line) instead of just line because of
the quotes and the \n in your output. Just use subnet.
You've also hardwired in the 100 and 200 values. Your target counts up
by 100 for each output line. Set a counter variable and increment it
after every line you print, and print the counter instead of the literal
100 etc.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list