实战Python:利用Python实现基于终端的文本行编辑程序
发布日期:2021-06-30 15:42:08
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月14日 13时21分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
xtrabackup备份之增量备份(二)
2021-07-04
《视频直播技术详解》系列:(6)编码和封装
2021-07-04
类函数重写、重载、覆盖示例
2021-07-04
五种主要多核并行编程方法分析与比较
2021-07-04
GB28181计算注册登陆时的鉴权信息
2021-07-04
人工智能为什么这么火?看看安防江湖30年血战就知道了
2021-07-04
“中国已建成世界上最大的视频监控网”背后的问题
2021-07-04
一个ps解复用H264和H264打包ps的动态库及demo
2021-07-04
“前端智能为安防产生新的数据价值”
2021-07-04
【转】 Linux那些事儿之我是U盘(20)冬天来了,春天还会远吗?(四)
2021-07-04
异或^ 的几个作用
2021-07-04
一个在pc上显示手机屏幕的小工具droid
2021-07-04
使用 ftrace 调试 Linux 内核
2021-07-04
gzip 命令
2021-07-04
Linux内核启动过程分析
2021-07-04
armv8 page table
2021-07-04
ARMv8的两种执行状态: AArch64/AArch32
2021-07-04
google protocol buffer
2021-07-04