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>In your case you should use <code>json</code> to do the data parsing. But if you have a problem with <code>json</code> (which is almost impossible since we are talking about an API ), then in general to convert from string to dictionary you can do:</p>

<pre><code>&gt;&gt;&gt; import ast
&gt;&gt;&gt; x = "{'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'}
"
&gt;&gt;&gt; d = ast.literal_eval(x)
&gt;&gt;&gt; d
</code></pre>

<p><code>d</code> now is a dictionary instead of a string.
<strong>But</strong> again if your data are in json format python has a built-in lib to handle  <a href="https://docs.python.org/2/library/json.html#module-json" rel="nofollow">json</a> format, <strong>and is better and safer to use json than ast.</strong></p>

<p>For example if you get a response let's say <code>resp</code> you could simply do:</p>

<pre><code>response = json.loads(resp)
</code></pre>

<p>and now you could parse <code>response</code> as a dictionary.</p>

python dictionary