【发布时间】:2022-01-21 11:06:56
【问题描述】:
因此,作为实习机会的一部分,我们被告知要制作一个待办事项列表 CLI 应用。问题是我不知道如何按优先级升序对文件中的文本进行排序,任何帮助将不胜感激
这是我的代码:
import typer
app = typer.Typer()
@app.command()
def help():
print("""Usage :-
$ ./todo add 2 hello world # Add a new item with priority 2 and text "hello world" to the list
$ ./todo ls # Show incomplete priortiy list items sorted by priority in ascending order
$ ./todo del NUMBER # Delete the incomplete item with the given priority number
$ ./todo done NUMBER # Mark the incomplete item with given PRIORITY_NUMBER as complete
$ ./todo help # Show usage
$ ./todo report # Statistics """)
@app.command()
def add(priority: int,task: str):
file1=open("ls.txt","r")
x=len(file1.readlines())
y=x+1
file1.close()
file1=open("ls.txt","a")
file1.write(str(y)+". "+task + " ["+str(priority)+"]"+ "\n")
file1.close()
@app.command()
def ls():
file1=open("ls.txt","r")
print(file1.read())
if __name__=="__main__":
app()
这是所需的输出image:
【问题讨论】:
-
请以文本而非图像的形式提供输出。
标签: python sorting command-line-interface typer