Converting to list of dictionary
<p>I have a text file filled with place data provided by twitter api. Here is the sample data of 2 lines</p>

<pre><code>{'country': 'United Kingdom', 'full_name': 'Dorridge, England', 'id': '31fe56e2e7d5792a', 'country_code': 'GB', 'name': 'Dorridge', 'attributes': {}, 'contained_within': [], 'place_type': 'city', 'bounding_box': {'coordinates': [[[-1.7718518, 52.3635912], [-1.7266702, 52.3635912], [-1.7266702, 52.4091167], [-1.7718518, 52.4091167]]], 'type': 'Polygon'}, 'url': 'https://api.twitter.com/1.1/geo/id/31fe56e2e7d5792a.json'}

{'country': 'India', 'full_name': 'New Delhi, India', 'id': '317fcc4b21a604d5', 'country_code': 'IN', 'name': 'New Delhi', 'attributes': {}, 'contained_within': [], 'place_type': 'city', 'bounding_box': {'coordinates': [[[76.84252, 28.397657], [77.347652, 28.397657], [77.347652, 28.879322], [76.84252, 28.879322]]], 'type': 'Polygon'}, 'url': 'https://api.twitter.com/1.1/geo/id/317fcc4b21a604d5.json'}
</code></pre>

<p>I want 'country', 'name' and 'cordinates' filed of each line.In order to do this we need to iterate line by line the entire file.so i append each line to a list</p>

<pre><code>data = []
with open('place.txt','r') as f:
    for line in f:
        data.append(line)
</code></pre>

<p>when i checked the data type it shows as 'str' instead of 'dict'.</p>

<pre><code>type(data[0])
str

data[0].keys()
AttributeError: 'str' object has no attribute 'keys'
</code></pre>

<p>how to fix this so that it can be saved as list of dictionaries.</p>

<p>Originally tweets were encoded and decoded by following code:</p>

<pre><code>f.write(jsonpickle.encode(tweet._json, unpicklable=False) + '\n') #encoded and saved to a .txt file
tweets.append(jsonpickle.decode(line)) # decoding
</code></pre>

<p>And place data file is saved by following code:</p>

<pre><code>fName = "place.txt"
newLine = "\n"
with open(fName, 'a', encoding='utf-8') as f:
    for i in range(len(tweets)):
        f.write('{}'.format(tweets[i]['place']) +'\n')
</code></pre>

<p>You can use list like this</p>

<pre><code>mlist= list() 
for i in ndata.keys(): 
    mlist.append(i)
</code></pre>

python dictionary