【发布时间】:2015-02-09 01:35:45
【问题描述】:
问题: 实现一个名为 stripComments(code) 的 Python 函数,其中 code 是一个参数,它接受一个包含 Python 代码的字符串。函数 stripComments() 返回删除所有 cmets 的代码。
我有:
def stripComments(code):
code = str(code)
for line in code:
comments = [word[1:] for word in code.split() if word[0] == '#']
del(comments)
stripComments(code)
我不确定如何具体告诉 python 搜索字符串的每一行,并在找到主题标签时删除该行的其余部分。 请帮忙。 :(
【问题讨论】:
-
举个例子会更好。
-
我没有给出一个例子......而且我不确定它应该是什么样子。
标签: string python-3.x comments