Python string to dictionary with json (not working)
<p>I have a text file like this :</p>

<p>"imei": "123456789",
"sim_no": "+90 xxx xxx xx xx",
"device_type": "standart",
"hw_version": "1.01",
"sw_version": "1.02"</p>

<p>and i want to convert to dictionary this file. Because i want to take values. </p>

<pre><code>import json
from time import sleep

def buffer(data):
    dicto=json.loads(data) 
    print(type(dicto))      

file=open("config.txt", "r").read()
jsondata=json.dumps(file)  
buffer(jsondata)

result : &lt;class 'str'&gt;
</code></pre>

<p>When i working in shell like this :</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; h = '{"foo":"bar", "foo2":"bar2"}'
&gt;&gt;&gt; type(h)
&lt;class 'str'&gt;
&gt;&gt;&gt; d=json.loads(h)
&gt;&gt;&gt; d
{'foo2': 'bar2', 'foo': 'bar'}
&gt;&gt;&gt; type(d)
&lt;class 'dict'&gt;
&gt;&gt;&gt; 
</code></pre>

<p>its working but i can't understand why my code not working. When i convert this file to dictionary i want to hold in a buffer. How can i hold this data inside array? Please excuse me i am new in Python.</p>

<p>I have to solve this question :</p>

<p>We have a text file like this :</p>

<pre><code>"imei": "123456789",
"sim_no": "+90 xxx xxx xx xx",
"device_type": "standart",
"hw_version": "1.01",
"sw_version": "1.02"
</code></pre>

<p>And we should read this JSON data, then we should read this data each 1 min and put to buffer(idk how can i put, is buffer array or dict?) then we should delete the oldest file each 5 min. Exceptions are important. Format sould be like "imei", "hw_version", "sw_version", "device_type". We sould solve buffer overflowing.</p>

<p>I write this code :</p>

<pre><code>import json
from time import sleep

def buffer(data):
    pass
    #imei = data.get("imei") # I want to read like this
    # this function should put the variables to array
counter=0
while True:
    with open("config.txt") as f:
        mydict = json.loads('{{ {} }}'.format(f.read()))    
    buffer(mydict)
    sleep(60)
    counter+=1
    if counter%5==0
        # delete the oldest data
</code></pre>

python json python-3.x