一個文本文件,在我的項目意見,我想提取從一個文本文件中的待辦事項清單。這是我迄今取得的進展。打印TODO:從Python中
這是todolist.txt
文本文件的內容
#TODO:example4
def printName():
name=input("Enter your name: ")
print("Hello " + name)
TODO:example3
def printNumbers():
for i in range (0,10):#TODO:example2
print(i)
printName()
printNumbers()
#TODO: example1
,這裏是與TODO提取行我的Python代碼:
file=open("todolist.txt","r")
word="TODO:"
for line in file:
if word in line:
print(line)
當我運行這個程序的結果是:
#TODO:example4
TODO:example3
for i in range (0,10):#TODO:example2
#TODO: example1
Process finished with exit code 0
所以我的問題是在這裏我想提取和打印TODO線只但你可以從上面看到,爲#TODO:例題我的程序印在spesific線前面的代碼了。
我想要做的是basicly只是打印TODO註釋。
找到#字符的索引並打印從這一點就行了。 用'line [index:]'其中index指向#字符。你可以找到它的查找方法,例如 – Ivan