Justin Wood
Justin Wood

Reputation: 50

How to fix line breaks not working using '\n' from python function with .join()

I have a function that when called loops through a dictionary object containing comments, and then should be displaying each one separated by a line.

I have searched through other topics on this I didn't find any one that answered for me.

I have tried '\n' '', but always get the text together.

def get_comment(stand):
    ''' Takes a comment code from the each of the comments
    field from inside the stand data and outputs
    a human readable text comment '''
    for _i in range(8):
        if stand:
            for _ in range(len(stand)):
                comments = []
                for _key, codes in stand.items():
                    comment = codes['carrier'] + ':' + \
                    codes['number'] + ' - ' + \
                    SamCode.objects.get(code=codes['dockingCode']).comment
                    comments.append(comment)

            return '\n'.join(comments)

This outputs

Eg.

AA:2776 - Manual start of system - ground AA:335 - Manual start of system - ground

whereas I need to to be

AA:2776 - Manual start of system - ground

AA:335 - Manual start of system - ground

Adding html template code for Django integration. Column.13 could have one comment or multiple as well as Column.14. Depending on the source of the comment.

   {% for column in performance.dailyDetails %}
    <td>{{ column.0 }}</td>
    <td>{{ column.1 }}</td>
    <td>{{ column.2 }}</td>
    <td>{{ column.3 }}</td>
    <td>{{ column.4 }}</td>
    <td>{{ column.5 }}</td>
    <td>{{ column.6 }}</td>
    <td>{{ column.7 }}</td>
    <td>{{ column.8 }}</td>
    <td>{{ column.9 }}</td>
    <td>{{ column.10 }}</td>
    <td>{{ column.11 }}</td>
    <td>{{ column.12 }}</td>
    <td>{% if column.13 == None %}
        {% else %}
          {{ column.13 }}<br>
        {% endif %}
        {% if column.14 == None %}
        {% else %}
          {{ column.14 }}<br>
        {% endif %}</td>

Here is one of the JSON dictionaries that are being looped over. IT shows the \n in the comment already, but it is not line breaking when output to HTML:

{'partition': 'AA',
 'date': '04/14/2019',
 'sortingName': 'ORD.H16', 
 'stand': 'ORD.H16', 
 'acType': 'A321,A321/2,B737/8-WL', 
 'inbound': 7, 
 'outbound': 2, 
 'blockIn': 4, 
 'blockOff': 2, 
 'slaInbound': 4, 
 'slaOutbound': 2, 
 'samInternal': None, 
 'sdkTechnical': None, 
 'ground': 'AA:2776 - Manual start of system - ground\nAA:335 - Manual start of system - ground', 
 'pilot': 'AA:362 - UNK - pilot', 
 'tower': None, 
 'operational': None, 
 'infrastructure': None, 
 'interface': None}

Upvotes: 0

Views: 527

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599956

This is entirely a matter of output. HTML ignores white space, including newlines; if you want data to appear in multiple lines you need to use HTML line break elements such as <br>.

There is a Django template filter that will translate newlines into HTML breaks: linebreaksbr.

{{ column.13|linebreaksbr }}

Upvotes: 2

Budvin Chathura
Budvin Chathura

Reputation: 1

make sure that all elements in comments list are string objects.

Regarding your question about adding a newline between comments, you got it almost right. If you print the returning value of the function, you will get the correct output

ex:

>>> x= '\n'.join(['1','2','3'])
>>> x
'1\n2\n3'
>>> print (x)
1
2
3

Upvotes: 0

Related Questions