Python read from a text file and create/return dictionary values
<p>I am having trouble creating a program that reads from text which maps a word and returns values. How would I write a code to read a text file and spit back a dictionary data structure. The text file is a lot longer than 3, but here is an example.</p>

<pre><code>Mary        21.0          25.0
Carson      25.0          27.0
Blair       22.0          10.0
</code></pre>

<p>and if I were to </p>

<pre><code>&gt;&gt;&gt; dict['Mary']
[21.0, 25.0]
</code></pre>

<p>and then</p>

<pre><code>&gt;&gt;&gt; dict['Mary'][0]
21.0

&gt;&gt;&gt; dict['Mary'][1]
25.0
</code></pre>

<p>Thanks.</p>

<p>If you text file is tab separated with one key/value pair per line you can use this (python 3.4+ only):</p>

<pre><code>my_dict = {}
with open('c:/path/to/file.txt', 'r') as fp:
    for line in fp:
        # grabs the first value as k and everything else a v
        k, *v = line.strip().split('\t')
        # turn the string values into floats
        my_dict[k] = tuple(map(float,v))
</code></pre>

python file dictionary