Python Forum
How to generate a log in a list style?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to generate a log in a list style?
#1
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]
Reply
#2
Insert some newlines in place of commas.

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

# Or

output.replace(",","\n")
Reply
#3
Replace +',' with +'\n'
Reply
#4
also look at logging module
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you guys, the '\n' solved my problem.
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter font style ATARI_LIVE 2 389 Oct-24-2020, 08:22 PM
Last Post: ATARI_LIVE
  C style strings deanhystad 2 803 May-16-2020, 02:57 PM
Last Post: Larz60+
  Type hints and style MaxPowers 1 762 Feb-19-2020, 06:56 PM
Last Post: micseydel
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 883 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 928 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Generate list in class P13N 7 2,079 Dec-28-2018, 10:08 PM
Last Post: P13N
  Generate a list of numbers within a list of arbitrary numbers Takeshio 4 1,545 Nov-08-2018, 12:29 AM
Last Post: Takeshio
  Is this good style? league55 3 1,598 Jan-14-2018, 04:25 PM
Last Post: Gribouillis
  How to generate the list I want? Krookroo 6 2,072 Sep-07-2017, 02:06 PM
Last Post: Krookroo
  pdf sms alternating conversation style sdcaliber 4 2,963 Apr-20-2017, 05:20 AM
Last Post: sdcaliber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020