简体   繁体   中英

Return a multiple value dictionary within a function into a pretty format

So the first 2 steps I took to come to this point were as follows:

  1. I opened a textfile. The print-method provided me with this:

     ["string1","a","b","c"] ["string2","d","e","f"] ["string3","g","h","i"] ["string4","j","k","l"] 
  2. I converted these lists into a dictionary. It looks like this now:

     dictionary = {"string1":["a","b","c"], "string2":["d","e","f"], "string3":["g","h","i"], "string4":["j","k","l"]} 

My goal was to return this dictionary within a function, so that it looks like this when it's printed in a main function:

{
"string1":["a","b","c"],
"string2":["d","e","f"],
"string3":["g","h","i"],
"string4":["j","k","l"]}

I tried applying a newline before each key but it only prints this:

{"\nstring1":["a","b","c"], "\nstring2":["d","e","f"],"\nstring3":["g","h","i"], 
"\nstring4":["j","k","l"]}

This is my function (including the main function):

import csv

def read_dictionary():
    with open("text.tsv") as tsvfile:
        tsvreader = csv.reader(tsvfile, delimiter = "\t")
        d = {}
        for line in tsvreader:
            first_row = line[0:1] 
            rest_rows = line[1:]
            for strings in first_row: #converting lists into strings
                d["\n"+strings] = rest_rows
        return d

if __name__=="__main__":
   print(read_dictionary())

Dictionaries (like all built-in containers) are represented with their contents shown as representations, essentially using the repr() function on each. For strings, those are meant to be as helpful as possible in that they are shown as string literals that can simply be copied and pasted to recreate their value. That means they also show unprintable characters or characters with special meaning as escape sequences . Your newline characters are just such characters.

In other words, you can't do what you want just by inserting \\n characters in the string values.

Instead, you need to do your own formatting if you really want to show your dictionary that way. Just print out the keys and values yourself:

def represent_dict(d):
    print('{', end='')  # no newline
    first = True
    for key, value in d.items():
        # newline at the start, not end, with commas
        print('{}\n{!r}: {!r}'.format('' if first else ',', key, value), end='')
        first = False
    print('}')  # closing with a brace and a newline

Remove the \\n addition in your reading code; it can be simplified to just produce the dictionary directly with a dictionary comprehension:

def read_dictionary():
    with open("text.tsv") as tsvfile:
        tsvreader = csv.reader(tsvfile, delimiter = "\t")
        return {row[0]: row[1:] for row in tsvreader}

represent_dict(read_dictionary())

You generally should keep presentation and data structure separate. Those newlines in the keys can easily cause issues elsewhere, and they are only there for the presentation output.

Demo of the output:

>>> dictionary = {"string1":["a","b","c"], "string2":["d","e","f"],
...               "string3":["g","h","i"], "string4":["j","k","l"]}
>>> represent_dict(dictionary)
{
'string1': ['a', 'b', 'c'],
'string2': ['d', 'e', 'f'],
'string3': ['g', 'h', 'i'],
'string4': ['j', 'k', 'l']}

While a very good answer is given, this would be an alternative method of printing the dictionary, by converting its key/item pairs to a list, same result, just less lines of code, this answer given as alternative:

def print_mydict(my_dict):
    my_list = list(my_dict.items())
    print('{')
    for i in my_list[:-1]:
        print('"{}":{},'.format(i[0], i[1]))
    print('"{}":{}}}'.format(my_list[-1][0], my_list[-1][1]))

but the last line looks more complex and less "readable", output the same:

{
"string1":['a', 'b', 'c'],
"string2":['d', 'e', 'f'],
"string3":['g', 'h', 'i'],
"string4":['j', 'k', 'l']}

您可以使用模板字符串或多个打印调用来实现此目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM