[Tutor] print issue

Mats Wichmann mats at wichmann.us
Wed Oct 2 13:57:10 EDT 2019


On 10/2/19 11:49 AM, Alan Gauld via Tutor wrote:
> On 02/10/2019 17:38, Alan Gauld via Tutor wrote:
>>
>> 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...
> 
> Not sure why the formatting got messed up, I'll try again!
> 
> with open('output.txt', 'r') as reader: counter = 100 fmtString =
> "%11s-[%d, 'tcp', 'allow', %s, null, null, %d, %d]\n" with open
> ('samplefile.txt', 'w') as p: for line in reader: address = line.strip()
> p.write(fmtString % (" ", counter, address, 500,500)) counter +=100
> p.write(fmtString % (" ", counter, address, 800,800)) counter +=100
> 
> I cheated slightly and moved the second 'open()' line up a bit too... :-)
> 

also note - there was a mention somewhere of seeking yaml-style output.

there is a yaml module which can help with the emitting part without 
having to worry about the formatting yourself (actually, to be honest, 
there are actually *many* yaml modules.  pyyaml is, I believe, the most 
popular - at least it's the one used by some very popular Python 
packages like django, docker compose, the kubernetes Python client, etc)






More information about the Tutor mailing list