File to dictionary
Question: > **Possible Duplicate:**  
>  [Python - file to
> dictionary?](http://stackoverflow.com/questions/4803999/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


Answer: 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)
    

