擅长:python、mysql、java
<blockquote>
<p>I would like to either modify the input string to place quotes around
each #xyz entry so that ast.literal_eval() will work, i.e. ( 'label',
'#75', '#174', '#196' )</p>
</blockquote>
<p>使用<code>re.sub</code>将#N替换为引号内的数字:</p>
<pre><code>>>> a = "( 'label', #75, #174, #196 )"
>>> a
"( 'label', #75, #174, #196 )"
>>> re.sub(r"#(\d+)", r"'#\1'", a)
"( 'label', '#75', '#174', '#196' )"
</code></pre>