python -- 基础知识测试
2017-11-29 11:18
253 查看
#! /usr/bin/python3 print("------read file first method------") tf = open('sketch.txt'); for echo_line in tf: if echo_line.find(':') >= 0: (role, line_spoken) = echo_line.split(':', 1); print(role, end=''); print(' said: ', end=''); print(line_spoken, end=''); tf.close(); print("------read file second method------") man = []; other = []; try: tf = open('sketch.txt'); for echo_line in tf: try: (role, line_spoken) = echo_line.split(':', 1); line_spoken = line_spoken.strip(); if role == 'Man' : man.append(line_spoken); elif role == 'Other Man' : other.append(line_spoken); except ValueError: pass tf.close(); print(man) print(other) except IOError: print('The file is missing.'); print("------read file third method------") man = []; other = []; try: tf = open('sketch.txt'); for echo_line in tf: try: (role, line_spoken) = echo_line.split(':', 1); line_spoken = line_spoken.strip(); if role == 'Man' : man.append(line_spoken); elif role == 'Other Man' : other.append(line_spoken); except ValueError: pass except IOError as err: print('The file is missing.' + str(err)); finally: if 'tf' in locals(): tf.close(); try: out_man = open('man_data.out', "w"); out_other = open('other_data.out', "w"); print(man, file=out_man) print(other, file=out_other) out_man.close(); out_other.close(); except IOError as err: print('File error' + str(err)); finally: if 'out_man' in locals(): out_man.close(); if 'out_other' in locals(): out_other.close(); print("------read file fourth method------") man = []; other = []; try: with open('sketch1.txt') as tf: for echo_line in tf: try: (role, line_spoken) = echo_line.split(':', 1); line_spoken = line_spoken.strip(); if role == 'Man' : man.append(line_spoken); elif role == 'Other Man' : other.append(line_spoken); except ValueError: pass print(man) print(other) except IOError as err: print('The file is missing.' + str(err)); print("------read file test use module:pickle ------") import pickle; man = ['I', 'Love', 'U']; try: with open('sketch.pickle', 'wb') as tf: #pickle.dump(man, tf); pickle.dump(['We', 'will', 'rock', 'you'], tf); with open('sketch.pickle', 'rb') as tf_r: other = pickle.load(tf_r); print(other) except pickle.PickleError as perr: print('The file is missing.' + str(perr));
相关文章推荐
- Python如何在dic(字典)里面添加dic(字典) 附dic基础知识
- 学学python(1)一些基础知识点
- python,linux学习基础知识
- python-基础知识之装饰器
- 软件测试基础知识复习
- python基础知识——内置数据结构(列表)
- 软件测试基础知识整理
- python基础知识补充
- python基础知识
- python基础知识笔记
- vim python自动补全方法 分类: ubuntu python基础学习 虚拟机 测试 2013-07-05 15:37 603人阅读 评论(0) 收藏
- python 基础知识
- python-关于类的几点基础知识
- [Python学习] 专题一.函数的基础知识
- Python 基础知识
- java基础知识测试
- 软件测试的基础知识
- python基础知识-函数
- Python学习笔记一(基础知识)
- 【python学习笔记一】程序设计计算机基础知识