【发布时间】:2017-08-09 13:31:57
【问题描述】:
在我的项目中,我想从文本文件中提取待办事项列表。这是我到目前为止的进展。
这是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 行 only 但正如您从上面看到的那样,对于 #TODO:example2 我的程序打印了前面的代码也在那条特定的线上。
我想做的基本上就是打印 TODO cmets。
【问题讨论】:
-
找到 # 字符的索引并打印从该点开始的行。
line[index:]其中 index 指向 # 字符。例如,您可以使用 find 方法找到它