Python - File to Dictionary with specific substrings for key value pairs
<p>I have a text file that looks like :</p>

<pre><code>AAAAA123123423452452BBBASDASAS323423423432
BBBBB453453453466123AAAAADDFFG6565656565665
</code></pre>

<p>...</p>

<p>I want to create a dictionary out of this, with keys the slices of each line like this :</p>

<p>file.txt :</p>

<pre><code>hash[123123] = "BBBASD"
hash[453453] = "AAAAAD"
</code></pre>

<p>I ve written the following try, but it doesnt work so far. Prints nothing :</p>

<pre><code>myhash{}
with open('file.txt') as fi:
    for life in fi:
        key = [line:6:13]
        value = [line:21:27]
        myhash[key] = value

print(myhash) 
</code></pre>

<p>Can someone help? Thanks in advance!</p>

<p>You should use:</p>

<pre><code>myhash = {}
with open('file.txt') as fi:
    for line in fi.readlines():
        key = line[5:11]
        value = line[20:26]
        myhash[key] = value

print(myhash) 
</code></pre>

<p>You can get <a href="http://stackoverflow.com/questions/509211/explain-pythons-slice-notation">more info here</a> about string slicing in Python.</p>

python compare comparison substring