-1

Possible Duplicate:
Python - file to dictionary?

I've been looking on this website, and racking my brain, but I just can't find the answer. I have a file of words that are matched with numbers, delimited with '*'. Now I have to find a way to convert this file to a dictionary in python and look up words from another text and assign the values from the dictionary file.

I know how to import a plain-text file, just not how I can make it "act like a dictionary" does anyone have any ideas?

Thanks everyone

Community
  • 1
  • 1
Shifu
  • 2,115
  • 3
  • 17
  • 15

1 Answers1

2

Assuming your file is one per line, e.g.

chicken*5
dog*3
...

You can do

with open("path/to/file") as f:
    data = [line.split("*") for line in f]
data = dict((word, int(cnt)) for (word, cnt) in data)
tom
  • 18,953
  • 4
  • 35
  • 35
  • Or you could do `data = {word: int(cnt) for line in f for word, cnt in line.split(*)}` so you don't need the intermediary list. – Volatility Jan 24 '13 at 00:12
  • @Volatility, Yep, sure can. I tend to avoid multiple `for`s in the same list comprehension, but in this case I agree your solution is pretty readable. `dict` comprehensions are also quite cool (for Python >= 2.7) – tom Jan 24 '13 at 00:16
  • Hey @Tom, thanks for the help, however if I try your code I get the error: ValueError: need more than 1 value to unpack. Do you know what that's about? – Shifu Jan 24 '13 at 00:18
  • Do you have lines in your file that do not fit the * format? Perhaps empty lines? You can ignore empty lines easily by doing `[line.split("*") for line in f if line.strip()]` in the second line. – tom Jan 24 '13 at 00:20
  • I get the same error: data = dict((word, int(cnt)) for (word, cnt) in data) ValueError: need more than 1 value to unpack – Shifu Jan 24 '13 at 00:26
  • The data is like this blabla*3 blablabla*3 5 – Shifu Jan 24 '13 at 00:26
  • Are they separated by newlines? And what's the '5' in the end of your example? – tom Jan 24 '13 at 00:28
  • there are sometimes several values next to words, and yes, a new line is started every time – Shifu Jan 24 '13 at 00:31
  • Several values next to words...how would you want to represent those? And are there *any* lines in your file that don't match that format, e.g. do not contain a *? – tom Jan 24 '13 at 00:35
  • Just checked the file, for some reason a whole bunch have no asterixes.... – Shifu Jan 24 '13 at 00:41
  • @Volatility has two syntax errors in the dict comprehension, which is confusing matters more. It should be like `{word: int(cnt) for line in f for word, cnt in (line.split('*'),)}` – wim Jan 24 '13 at 01:09