如何使用正则表达式跳过文档字符串
时间:2020-03-06 14:57:50 来源:igfitidea点击:
我正在尝试将一些导入行插入到python源文件中,但是理想情况下,我想将它们放置在初始文档字符串之后。假设我将文件加载到这样的lines变量中:
lines = open('filename.py').readlines()
如何找到行号,文档字符串在哪里结束?
解决方案
如果我们使用的是标准文档字符串格式,则可以执行以下操作:
count = 0 for line in lines: if line.startswith ('"""'): count += 1 if count < 3: # Before or during end of the docstring continue # Line is after docstring
可能需要对没有文档字符串的文件进行一些调整,但是如果文件格式一致,则应该很容易。
可以使用python的tokenize模块,而不是使用正则表达式或者依赖于特定格式,而无需使用正则表达式。
import tokenize f=open(filename) insert_index = None for tok, text, (srow, scol), (erow,ecol), l in tokenize.generate_tokens(f.readline): if tok == tokenize.COMMENT: continue elif tok == tokenize.STRING: insert_index = erow, ecol break else: break # No docstring found
这样,我们甚至可以处理以下病理情况:
# Comment # """Not the real docstring""" ' this is the module\'s \ docstring, containing:\ """ and having code on the same line following it:'; this_is_code=42
就像python一样处理它们。