問題: 實現一個名爲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)
我不知道如何明確告訴蟒蛇通過串的每一行進行搜索,當它找到一個主題標籤,刪除該行的其餘部分。 請幫忙。 :(
一個例子會更好。 – 2015-02-09 01:37:12
我沒有得到一個例子......我不知道它應該如何看待。 – 2015-02-09 01:46:26