-7

我想知道如何

  1. 判断(True / False)一行Python代码是否有注释
  2. 将线分成code,comment

例如:

loc_1 = "print('hello') # this is a comment"

非常简单,但是例如:

loc_2 = for char in "(*#& eht # ": # pylint: disable=one,two # something

不是那么直截了当。我一般不知道该怎么做,这样我就可以做到

f(loc_2)
# returns
[
    # code
    'for char in "(*#& eht # ":',
    # comment
    ' # pylint: disable=one,two # something'
]

来自评论:“你用 libcs​​t 标记了这个。你已经使用那个库给你一个 AST 了吗?”

我曾尝试使用它但失败了,例如:

来自评论:“您是在解析单行代码、函数或类的源代码,还是解析整个模块?

我正在解析单行 - 至少这是我的意图。我希望能够遍历文件中的行。我有一个预先存在的过程,它已经迭代了 python 文件的行,但我想扩展它以考虑引发这个问题的评论。

来自评论“您对解析结果的最终目标是什么,只需获取没有评论的源代码?”

不 - 我想要示例中给出的源代码和注释。

4

1 回答 1

-1

io.StringIO()我认为您可以使用and找到答案的好方法tokenize.generate_tokens()。如果应用于表示一行 python 的字符串,您应该得到一个列表,tokeninfo然后您可以检查注释标记 ( tokenize.COMMENT)。

您的f()方法可能看起来有点像这样,但请使名称有意义而不是f()

import io
import tokenize

def separate_code_from_comments(text):
    reader = io.StringIO(text).readline
    comment_tokens = (t for t in tokenize.generate_tokens(reader) if t.type == tokenize.COMMENT)
    comment = next(comment_tokens, None)
    return [text[0: comment.start[1]], text[comment.start[1]:]] if comment else [text, '']

print(separate_code_from_comments("print('hello') # this is a comment"))
print(separate_code_from_comments("# this is a comment"))
print(separate_code_from_comments("loc_2 = for char in \"(*#& eht #\": # pylint: disable=one,two # something"))
print(separate_code_from_comments("loc_2 = for char in \"(*#& eht #\": "))

这应该打印出来:

["print('hello') ", '# this is a comment']
['', '# this is a comment']
['loc_2 = for char in "(*#& eht #": ', '# pylint: disable=one,two # something']
['loc_2 = for char in "(*#& eht #": ', '']
于 2021-08-21T19:17:29.153 回答