Python Forum
How to generate a log in a list style? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to generate a log in a list style? (/thread-26122.html)



How to generate a log in a list style? - wagnergt12 - Apr-21-2020

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]


RE: How to generate a log in a list style? - stullis - Apr-21-2020

Insert some newlines in place of commas.

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

# Or

output.replace(",","\n")



RE: How to generate a log in a list style? - deanhystad - Apr-21-2020

Replace +',' with +'\n'


RE: How to generate a log in a list style? - buran - Apr-22-2020

also look at logging module


RE: How to generate a log in a list style? - wagnergt12 - Apr-22-2020

Thank you guys, the '\n' solved my problem.


RE: How to generate a log in a list style? - buran - Apr-22-2020

(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