小编Des*_*own的帖子

如何从字符串中删除“#”注释?

问题:实现一个名为 stripComments(code) 的 Python 函数,其中 code 是一个参数,它采用包含 Python 代码的字符串。函数 stripComments() 返回删除所有注释的代码。

我有:

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)
Run Code Online (Sandbox Code Playgroud)

我不确定如何具体告诉 python 搜索字符串的每一行,并在找到主题标签时删除该行的其余部分。请帮忙。:(

string comments python-3.x

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

comments ×1

python-3.x ×1

string ×1