Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are splitting your line into a list of words, but only giving it one key and value.</p> <p>This will work:</p> <pre><code>with open('LIWC_words.txt', 'r') as document: answer = {} for line in document: line = line.split() if not line: # empty line? continue answer[line[0]] = line[1:] </code></pre> <p>Note that you don't need to give <code>.split()</code> an argument; without arguments it'll both split on whitespace <em>and strip the results for you</em>. That saves you having to explicitly call <code>.strip()</code>.</p> <p>The alternative is to split only on the first whitespace:</p> <pre><code>with open('LIWC_words.txt', 'r') as document: answer = {} for line in document: if line.strip(): # non-empty line? key, value = line.split(None, 1) # None means 'all whitespace', the default answer[key] = value.split() </code></pre> <p>The second argument to <code>.split()</code> limits the number of splits made, guaranteeing that there at most 2 elements are returned, making it possible to unpack the values in the assignment to <code>key</code> and <code>value</code>.</p> <p>Either method results in:</p> <pre><code>{'aaien': ['12', '13', '39'], 'aan': ['10'], 'aanbad': ['12', '13', '14', '57', '58', '38'], 'aanbaden': ['12', '13', '14', '57', '58', '38'], 'aanbeden': ['12', '13', '14', '57', '58', '38'], 'aanbid': ['12', '13', '14', '57', '58', '39'], 'aanbidden': ['12', '13', '14', '57', '58', '39'], 'aanbidt': ['12', '13', '14', '57', '58', '39'], 'aanblik': ['27', '28'], 'aanbreken': ['39']} </code></pre> <p>If you still see only <em>one</em> key and the rest of the file as the (split) value, your input file is using a non-standard line separator perhaps. Open the file with <a href="http://docs.python.org/2/glossary.html#term-universal-newlines" rel="nofollow">universal line ending support</a>, by adding the <code>U</code> character to the mode:</p> <pre><code>with open('LIWC_words.txt', 'rU') as document: </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload