So the first 2 steps I took to come to this point were as follows:
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"]
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())