擅长:python、mysql、java
<p>我建议您查看Python的PLY文档,看看andrewdalke的例子
对于分子的解析例子。(<a href="http://www.dalkescientific.com/writings/NBN/parsing_with_ply.html" rel="nofollow">http://www.dalkescientific.com/writings/NBN/parsing_with_ply.html</a>)</p>
<p>您可以定义带有原子符号和该原子/符号出现在分子中的时间的代币,例如,对于CH3COOH(醋酸)这样的分子</p>
<pre><code>import lex
tokens = (
"SYMBOL",
"COUNT"
)
t_SYMBOL = (
r"C[laroudsemf]?|Os?|N[eaibdpos]?|S[icernbmg]?|P[drmtboau]?|"
r"H[eofgas]?|A[lrsgutcm]|B[eraik]?|Dy|E[urs]|F[erm]?|G[aed]|"
r"I[nr]?|Kr?|L[iaur]|M[gnodt]|R[buhenaf]|T[icebmalh]|"
r"U|V|W|Xe|Yb?|Z[nr]"
)
def t_COUNT(t):
r"\d+"
t.value = int(t.value)
return t
lex.lex()
lex.input("CH3COOH")
for tok in iter(lex.token, None):
print repr(tok.type), repr(tok.value)
</code></pre>
<p>当我运行代码时,我得到了以下信息</p>
<p>'符号'C'
'符号'H'
“计数”3
'符号'C'
'符号'O'
'符号'O'
'符号'H'</p>
<p>更多信息请点击此处
<a href="http://www.dabeaz.com/ply/" rel="nofollow">http://www.dabeaz.com/ply/</a></p>