-1

I can't find any way to add data to dictionaries from text documents. Wait, is it even possible?

e.g.

with open("D:\Programming - Python\S.A.M.I\Words/Pos.txt", encoding="utf-8") as f:
posread=f.read()

(and then have this split each word using .split(,) to a dictionary)

Mazdak
  • 105,000
  • 18
  • 159
  • 188
SirBuncey
  • 67
  • 1
  • 2
  • 9
  • What's the problem with any existing code? What's the input - what's the expected output... etc... – Jon Clements Mar 26 '15 at 12:35
  • I needed to compare to lots of data to output the similar words in both. I tried using lists and tuples with no result and now I'm trying dictionaries. I have a file of words which I wish to split into a dictionary and then split the words of the users input into a dictionary. Then I will be able to run and see which (if any) words feature in both the text document and the users input. – SirBuncey Mar 26 '15 at 12:37

1 Answers1

0

If your input in.txt is in the following format:

val1 val2
val3 val4

you can read it in the following way:

f = file("in.txt")
d = {}
while True:
    line = f.readline()
    if not line:
        break
    c0,c1 = line.split()
    d[c0] = c1

...and now you have the dictionary d.

juhist
  • 4,210
  • 16
  • 33