Python parse string into Python dictionary of list
<p>There are two parts to this question:</p>

<p><strong>I. I'd like to parse Python string into a list of dictionary.</strong> </p>

<p>****Here is the Python String****</p>

<pre><code>../Data.py:92 final computing result as shown below:  [historic_list {id: 'A(long) 11A' startdate: 42521 numvaluelist: 0.1065599566767107 datelist: 42521}historic_list {id: 'A(short) 11B' startdate: 42521 numvaluelist: 0.0038113334533441123 datelist: 42521 }historic_list {id: 'B(long) 11C' startdate: 42521 numvaluelist: 20.061623176440904 datelist: 42521}time_statistics {job_id: '' portfolio_id: '112341'} UrlPairList {}]
</code></pre>

<p>****Expected Python Output:****</p>

<pre><code>{
  "data" :[
    {
      "id": "A(long) 11A"
      "startdate": "42521"
      "numvaluelist": "0.1065599566767107"
    },
    {
      "id": "A(short) 11B"
      "startdate": "42521"
      "numvaluelist": "0.0038113334533441123"
    },
    {
      "id": "B(long) 11C"
      "startdate": "42521"
      "numvaluelist": "20.061623176440904"
    }
  ]
}
</code></pre>

<p><strong>II. I need to further parse key values of id and numvaluelist. I am not sure if there is a better way to do it. Hence, I am converting string to Python Dictionary, loop through that and parse further. Please guide me if I am overthinking the solution.</strong></p>

<p><strong>Update: Code</strong></p>

<pre><code>text = "[historic_list {id: 'A(long) 11A' startdate: 42521 numvaluelist: 0.1065599566767107 datelist: 42521}historic_list {id: 'A(short) 11B' startdate: 42521 numvaluelist: 0.0038113334533441123 datelist: 42521 }historic_list {id: 'B(long) 11C' startdate: 42521 numvaluelist: 20.061623176440904 datelist: 42521}time_statistics {job_id: '' portfolio_id: '112341'} UrlPairList {}]"
data = text.strip("../Data.py:92 final computing result as shown below:  ")
print data
</code></pre>

<p>Your input raw text looks pretty predictable, try this:</p>

<pre><code>&gt;&gt;&gt; import re

&gt;&gt;&gt; raw = "[historic_list {id: 'A(long) 11A' startdate: 42521 numvaluelist: 0.1065599566767107 datelist: 42521}historic_list {id: 'A(short) 11B' startdate: 42521 numvaluelist: 0.0038113334533441123 datelist: 42521 }historic_list {id: 'B(long) 11C' startdate: 42521 numvaluelist: 20.061623176440904 datelist: 42521}time_statistics {job_id: '' portfolio_id: '112341'} UrlPairList {}]"

&gt;&gt;&gt; line_re = re.compile(r'\{[^\}]+\}')
&gt;&gt;&gt; records = line_re.findall(raw)

&gt;&gt;&gt; record_re = re.compile(
...     r"""
...             id:\s*\'(?P&lt;id&gt;[^']+)\'\s*
...             startdate:\s*(?P&lt;startdate&gt;\d+)\s*
...             numvaluelist:\s*(?P&lt;numvaluelist&gt;[\d\.]+)\s*
...             datelist:\s*(?P&lt;datelist&gt;\d+)\s*
...             """,
...     re.X
...     )

&gt;&gt;&gt; record_parsed = record_re.search(line_re.findall(raw)[0])
&gt;&gt;&gt; record_parsed.groupdict()
{'startdate': '42521', 'numvaluelist': '0.1065599566767107', 'datelist': '42521', 'id': 'A(long) 11A'}

&gt;&gt;&gt; for record in records:
...     record_parsed = record_re.search(record)
...     # Here is where you would do whatever you need with the fields.
</code></pre>

<p>To parse the subelements of the id, e.g.:</p>

<pre><code>&gt;&gt;&gt; record_re2 = re.compile(
...     r"""
...             id:\s*\'
...                     (?P&lt;id_letter&gt;[A-Z]+)
...                     \(
...                             (?P&lt;id_type&gt;[^\)]+)
...                             \)\s*
...                     (?P&lt;id_codenum&gt;\d+)
...                     (?P&lt;id_codeletter&gt;[A-Z]+)
...                     \'\s*
...             startdate:\s*(?P&lt;startdate&gt;\d+)\s*
...             numvaluelist:\s*(?P&lt;numvaluelist&gt;[\d\.]+)\s*
...             datelist:\s*(?P&lt;datelist&gt;\d+)\s*
...             """,
...     re.X
...     )

&gt;&gt;&gt; record2_parsed = record_re2.search(line_re.findall(raw)[0])
&gt;&gt;&gt; record2_parsed.groupdict()
{'startdate': '42521', 'numvaluelist': '0.1065599566767107', 'id_letter': 'A', 'id_codeletter': 'A', 'datelist': '42521', 'id_type': 'long', 'id_codenum': '11'}
</code></pre>

python list loops dictionary