Banthita Limwilai
Banthita Limwilai

Reputation: 181

How to insert new line between Python dict value

I'm trying to manipulate the dict output that showing in the html page using Flask and Jinja2 template.

I'm looking for help with

  1. Adding new lines between every dict values.
  2. make http text clickable for https://example.com:8087

The way I created my dictionary is

usedPort[node][z_port] =  (z_owner, docker_stack, url)

The expectation of the result is

john_doe
Zeppelin-Engineer-Individual-TAP
https://example.com:8087

But actually, I got

(john_doe, Zeppelin-Engineer-Individual-TAP, https://example.com:8087)

There's nothing involved print operation, I don't want to print the output in the terminal but want to show this dict value in the html page instead.

for http text, I've tried with webbrowser module unfortunately, It didn't work.

Upvotes: 0

Views: 1060

Answers (1)

kabanus
kabanus

Reputation: 25980

You are using a tuple, and you do not tell how you display it. If you simply pass the tuple to something that displays it (be it print or anything else) it will use the default representation, which is what you get.

Instead, pass what you want to actually represent:

'\n'.join(str(x) for x in my_tuple)  # Can use use `'\n'.join(my_tuple) if everything is a string

For some overkill you can define your own set (using collection.UserTuple or just inheriting from tuple which could create some problems for some uses)

class Tuple(tuple):
    def __repr__(self)
        return '\n'.join(str(x) for x in self)

The you would have to use Tuple(...) instead of just (...), but be default you would get newlines between values anywhere.

Upvotes: 2

Related Questions