0

I've got an array in a funcion...

for i in data:
    dates.append(str(i['month'])+": "+str(i['the_days']))

that I'm pulling into the body of an email like this...

date_list = avDates()
date_list_string = ' '.join(date_list)

html = """"<html>
<head></head>
<body>
<p> These are the dates: </p>
""" + date_list_string + """
</body>
<html>
"""

and this is returning the data as a string within the email...

Apr: 16, 29 May: 13, 27 Jun: 10, 11, 24 etc

How can I change the code so that the string is shown in tabular for or with a line break after each i ?

Apr: 16, 29 
May: 13, 27 
Jun: 10, 11, 24 
etc

I've tried putting "/n" in various places but it just prints out rather than being executed.

2 Answers 2

4

Join your dates list with a newline character \n instead of a space:

date_list_string = '\n'.join(date_list)

This will create one date element per line in the string output. But as you're using this to build HTML, you (also or instead of that) need to insert HTML line-break tags:

date_list_string = '<br/>'.join(date_list)

Or with both line-breaks in the string and HTML:

date_list_string = '<br/>\n'.join(date_list)
Sign up to request clarification or add additional context in comments.

Comments

2

If you're printing to terminal, "\n" means a newline. In HTML, <br> or <br/> (XHTML) is used for line breaks instead. So:

for i in data:
    dates.append(str(i['month'])+": "+str(i['the_days']) + "<br>")

# OR

for i in data:
    dates.append("{}: {}<br>".format(str(i['month']), str(i['the_days']))

# OR 

date_list_string = '<br>'.join(date_list)

1 Comment

@bruno desthuilliers Thanks for fixing the typo! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.