Regex add quotes in Python so I can return Python Dictionary
<p>Using Python and Regex, I would like to add quotes to each word. Currently, I am only able to add quotes to a first index. When I loop through my end results, I get a string. Instead, I would like Python Dictionary. To solve this problem, I think adding quotes will help me get dictionary instead of a string. Can someone please guide me? </p>

<p><strong>Code</strong></p>

<pre><code>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 {}]"

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

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
    )

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

for record in records:
    record_parsed = record_re.search(record)
    print type(record)
</code></pre>

<p><strong>Current Output</strong></p>

<pre><code>{id: 'A(long) 11A' startdate: 42521 numvaluelist: 0.1065599566767107 datelist: 42521}
{id: 'A(short) 11B' startdate: 42521 numvaluelist: 0.0038113334533441123 datelist: 42521 }
{id: 'B(long) 11C' startdate: 42521 numvaluelist: 20.061623176440904 datelist: 42521}
</code></pre>

<p><strong>Desired Output</strong> Everything in quotes</p>

<pre><code>{'id': 'A(long) 11A' 'startdate': '42521' 'numvaluelist': '0.1065599566767107' 'datelist': '42521'}
{'id': 'A(short) 11B' 'startdate': '42521' 'numvaluelist': '0.0038113334533441123' 'datelist': '42521' }
{'id': 'B(long) 11C' 'startdate': '42521' 'numvaluelist': '20.061623176440904' 'datelist': '42521'}
</code></pre>

<p>Here's a way to parse it using regular expressions. If you are matching each part so exactly as in your original, there's probably a more generic way.</p>

<pre><code>import re
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 {}]"

line_re = re.compile(r'\{[^\}]+\}')
value_re = re.compile(r"(\w+): ('[^']*'|\S+)")

data = []
lines = line_re.findall(raw)
for line in lines:
    data_line = dict()
    values = re.findall(value_re, line)
    for (name, value) in values:
        if(value[-1] == '}'): value = value[:-1]  # to handle "foo}" without space
        if(value[:1] == "'"): value = value[1:-1]  # strip quotes
        data_line[name] = value
    data.append(data_line)

print data
</code></pre>

python regex for-loop dictionary