From dictionary to plain text

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cheo

Is there a way to print the content of a dictionary in plain text excluding the json format?

print(JSON.print(dictionary, "\t")) will return:

	"Item1": 4,
	"Item2": 3

print(dictionary) will return:


I’d like to have it formatted like this:

Item1 : 4
Item2 : 3

The result will be used in a label to list all the content of the dictionary.

This code works for the console, but not for the label, as it only shows the last key:value pair of the dictionary:

for i in dictionary:
    print("%s : %d" % [i, dictionary[i]]) 

So I’m struggling to translate this to a label

:bust_in_silhouette: Reply From: Skyfrit

Here is the code:

func _ready():
	var test:Dictionary = {"Item1": 4, "Item2": 3}
	for i in test:
		$Label.text += "%s : %d" % [i, test[i]]
		if (i != test.keys().back()):
			$Label.text += "\n"

That made the trick. Thanks!

cheo | 2020-12-07 13:55