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>This looks like an <a href="http://xyproblem.info/" rel="nofollow">XY Problem</a>. Your end goal is to parse that textual data into Python dictionaries. Adding quotes is the way you've come up with to do that (presumably you're then planning to use <code>eval()</code> to parse it), but it's the long way around.</p>

<p>Instead, parse it directly. You don't even need a regex, and it is much clearer what you're doing. Here's a quick and dirty attempt.</p>

<pre><code>from collections import OrderedDict

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

record = OrderedDict()
records = []

tokens = iter(raw.split())
previous_token = ""

for token in tokens:
    if previous_token == "{id:":
        record["id"] = token.lstrip("'")
        # get the rest of the ID up to closing quote
        for token in tokens:
            record["id"] += " " + token
            if token.endswith("'"):
                record["id"] = record["id"].rstrip("'")
                break
    elif previous_token in ("startdate:", "numvaluelist:"):
        record["numvaluelist"] = token
    elif previous_token == "datelist:":
        record["datelist"] = token.partition("}")[0]
        # record is complete; start new one
        records.append(record)
        record = OrderedDict()
    previous_token = token
</code></pre>

<p>Once you have it as Python data, you can of course print it any way you like... including, just for fun, the format you asked for:</p>

<pre><code>for record in records:
    print("{%s}" % ", ".join(repr(k) + ": " + repr(record[k]) for k in record))
</code></pre>

python regex for-loop dictionary