我想从重构文本字符串中的代码指令逐字提取源代码。
以下是我第一次尝试这样做,但我想知道是否有更好的(即更强大,更通用或更直接)的方式。
假设我在python中将以下第一个文本作为字符串:
s = '''
My title
========
Use this to square a number.
.. code:: python
def square(x):
return x**2
and here is some javascript too.
.. code:: javascript
foo = function() {
console.log('foo');
}
'''
要获得两个代码块,我可以
from docutils.core import publish_doctree
doctree = publish_doctree(s)
source_code = [child.astext() for child in doctree.children
if 'code' in child.attributes['classes']]
现在 source_code 是一个列表,其中只包含两个代码块的逐字源代码。如有必要,我还可以使用 child 的属性属性来查找代码类型。
它完成了这项工作,但还有更好的方法吗?
答案 0 :(得分:0)
这可以进一步简化,例如:
=IFNA(VLOOKUP(A2,Fruits!$A$2:$B$200,2,FALSE),VLOOKUP(A2,Vegetables!$A$2:$B$200,2,FALSE))