实战Python:利用Python实现基于终端的文本行编辑程序
发布日期:2021-06-30 15:42:08 浏览次数:2 分类:技术文章

本文共 3353 字,大约阅读时间需要 11 分钟。

在这里插入图片描述

程序课程设计最后的随堂测试,对于面向对象编程我理解的还不够透彻,于是和铁憨憨同学用面向过程写的= =。

def show_(text, op):    """     显示文本函数    :param text: 当前文本     :param op: 操作码    """    def show_all(text):        """        显示所有文本内容        :param text: 当前文本        """        for x in text:            print(x)    def show_part(text, line1, line2):        """        先是从line1到line2的文本内容        :param text: 当前文本        :param line1: 显示起始行        :param line2: 显示终止行        """        for i in range(line1-1, line2):            print(text[i])    # 用split对操作码字符串进行分割,进行后续判断    ops = op.split(" ")    if len(ops) == 1:        show_all(text)    else:        show_part(text, int(ops[1]), int(ops[2]))def add_(text, op):    """    增加文本函数    :param text: 当前文本    :param op: 操作码    :return: 增加后文本    """    def add_end(text, s):        """        在当前文本末尾追加一行指定文本        :param text: 当前文本        :param s: 追加文本        :return: 增加后文本        """        text.append(s)        return text    def add_ends(text):        """        输入多行文本,并追加到末尾        :param text: 当前文本        :return: 增加后文本        """        while True:            s = input()            # 通过输入###进行输入终止            if s == "###":                break            text.append(s)        return text    ops = op.split(" ", 1)    if len(ops) == 1:        text = add_ends(text)    else:        text = add_end(text, ops[1])    return textdef insert_(text, op):    """    插入文本函数    :param text: 当前文本    :param op: 操作码    :return: 插入后文本    """    def insert_in(text, line, s):        """        在当前文本指定行插入指定文本        :param text: 当前文本        :param line: 指定行        :param s: 插入文本        :return: 插入后文本        """        text.insert(int(line), s)        return text    ops = op.split(" ", 1)    text = insert_in(text, ops[1], ops[2])    return textdef del_(text, op):    """    删除文本函数    :param text: 当前文本    :param op: 操作码    :return: 删除后文本    """    def del_one(text, line):        """        删除一行        :param text: 当前文本        :param line: 删除行数        :return: 删除后文本        """        del text[line]        return text    def del_lot(text, line1, line2):        """        :param text: 当前文本内容        :param line1: 删除起始行        :param line2: 删除终止行        :return: 删除后文本        """        return [x for (i, x) in enumerate(text) if i < line1-1 or i > line2-1]    ops = op.split(" ")    if len(ops) == 2:        text = del_one(text, int(ops[1]))    else:        text = del_lot(text, int(ops[1]), int(ops[2]))    return textdef write_(text, op):    """    写文件,在文件末尾加入新的文本内容    实现保存当前文本到指定文件    :param text: 需要写入的文本内容    :param op: 操作码    """    file_path = op.split(" ")[1]    with open(file_path, 'w') as f:        f.write("\n".join(text))def read_(op):    """    读文件操作    实现加载指定文件为当前文本    :param op: 操作码    :return: 按行分割后的文本内容    """    file_path = op.split(" ")[1]    with open(file_path, 'r') as f:        text = f.read().splitlines()        return textdef main():    text = []    while True:        op = input()        if op.startswith("l"):            show_(text, op)        elif op.startswith("a"):            text = add_(text, op)        elif op.startswith("i"):            text = insert_(text, op)        elif op.startswith("d"):            text = del_(text, op)        elif op.startswith("r"):            text = read_(op)        elif op.startswith("w"):            write_(text, op)        elif op.startswith("q"):            breakmain()

属实铁憨憨

转载地址:https://kaisarh.blog.csdn.net/article/details/102933096 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Python:学习笔记之PyQt5
下一篇:爬虫:Python爬虫学习笔记之网页解析基础——爬取360导航栏目

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月12日 17时08分28秒