Python Forum

Full Version: How to generate a log in a list style?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Guys, I have a code for bkp my network devices, I have placed some improvement to generate a log for document the failed devices, so I can check it manually.
My code is generating the output I want in a text, however I'd like to improve the formatting...

The code generates the log file in one single line and my goal is to generate a list instead.

my code is:
except Exception as ex:
           
            print('An exception occurred.  Details', ex)
            output2 = ip + ','
            print output2
            filename2 = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix2,now.day,now.month,now.year,now.hour,now.minute,now.second)
            ff = open(filename2, 'a')
            ff.write(output2)
            ff.close()
            f.close() 
and I got the text file like this:
[inline]
192.168.60.3,10.185.88.1,10.147.63.253,10.0.15.253,172.30.4.4,172.30.7.3,172.30.7.4,10.177.105.2,10.156.63.253,192.168.224.241,
[/inline]

Id like to getsomething like that:

[inline]
192.168.60.3,
10.185.88.1,
10.147.63.253,
10.0.15.253,
172.30.4.4,
172.30.7.3,
172.30.7.4,
10.177.105.2,
10.156.63.253,
192.168.224.241,
[/inline]
Insert some newlines in place of commas.

"\n".join(output.split(","))

# Or

output.replace(",","\n")
Replace +',' with +'\n'
also look at logging module
Thank you guys, the '\n' solved my problem.
(Apr-22-2020, 12:41 PM)wagnergt12 Wrote: [ -> ]Thank you guys, the '\n' solved my problem.
it solved your problem. However if you want to do logging properly, look at the logging module