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
  Python Style and Naming Variables rsherry8 3 268 Jun-07-2021, 09:30 PM
Last Post: deanhystad
  Tkinter font style ATARI_LIVE 2 325 Oct-24-2020, 08:22 PM
Last Post: ATARI_LIVE
  C style strings deanhystad 2 738 May-16-2020, 02:57 PM
Last Post: Larz60+
  Type hinting style and PEP8 hlovatt 2 814 May-07-2020, 08:12 PM
Last Post: hlovatt
  Type hints and style MaxPowers 1 715 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 785 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 868 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Merging Dictionaries - Optimum Style? adt 5 917 Oct-09-2019, 05:26 PM
Last Post: adt
  Generate list in class P13N 7 1,950 Dec-28-2018, 10:08 PM
Last Post: P13N
  Generate a list of numbers within a list of arbitrary numbers Takeshio 4 1,435 Nov-08-2018, 12:29 AM
Last Post: Takeshio

Forum Jump:

User Panel Messages

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