Newline within list not printing on separate line

81 Views Asked by At

I am taking a unicode text data type and then parsing through it via Regex to find information and appending it to a list. The data in this list can very but there is a known object that I always want to start on a separate line. I tried adding a new line character to my list at this point and it always adds the newline character in the correct place but it seems that this character is just treated as an object in the list when printed and not actually forcing the output of the print to be on a separate line. Does anyone have any suggestions on how I can force this data on to another line?

=============== CURRENT PROGRAM OUTPUT ALL ON SAME LINE ==============

['\n', u'10.41.71.19 ', u'64512', '\n', u'192.168.0.1 ']

=============== DESIRED OUTPUT ON TWO LINES ==============

[u'10.41.71.19 ', u'64512', 

u'192.168.0.1 ']

============= SNIPIT WITH CODE WHERE I AM ADDING NEW LINE TO MY LIST ======

def jnprBgp(output):

 for line in output.splitlines():
    peerip = re.search('(\d+.\d+.\d+.\d+.)', line)
    if peerip:
       testlist.append('\n')
       testlist.append(peerip.groups()[0])
    peeras = re.search('\d+.\d+.\d+.\d+.\s+(\d+)', line)
    if peeras:
       testlist.append(peeras.groups()[0])

 for line in testlist:
    print testlist
1

There are 1 best solutions below

0
On

printing a list with the string '\n' as an element of it will not result in the printing of an actual newline character. That means you should handle the printing of the list yourself, like so:

def printList(l):
    print '[%s]' % ', '.join([repr(elem) if elem != '\n' else '\n' for elem in l])