split line by line from file and convert it into dictionary

  dictionary, python

Here the function where it read file and split it, i tried using split(‘n) but the out put still the same

def take_words(here):
d ={}

for word in here.splitlines():    
    if word not in d:
        d[word] = 1
else:
    d[word] += 1
    print(d)

savefile = open("result.txt",'w')
savefile.write(str(d))
savefile.close
return d

the initial file is

this me
this you

but the output is

this me :1
me this :1
this you :1

i want to make it like this in the dictionary

this me:1
this you:1

Source: Python Questions

LEAVE A COMMENT