datafile.txt  #文件

Man: this is the right room for an argument.
Other Man: I've told you once.
Man: No you haven't
Other Man: Yes, I have.
(pause)
Man: When?
Other Man: Just now.
Man: No you didn't
Other Man: Yes I did.
Man: You didn't
Other Man: I'm telling you, I did!

nester.py #输出模块 第2章文件nester.py修改后的模块,安装到你的python环境中

 def print_lol(the_list, indent = False, level = 0, fn = sys.stdout):
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item, indent, level + 1, fn);
else:
if indent:
for tab_stop in range(level):
print("\t",end='', file = fn)
print(each_item, file = fn)

sketch.py #读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out

 import nester
man = []
other = []
try:
data = open ("datafile.txt") for each_line in data:
try:
(role, line_spoken) = each_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 data.close()
except IOError:
print('this data file is missing!') try:
with open('man.out', 'w') as man_out, open('other.out','w') as other_out:
nester.print_lol(man, fn = man_out)
nester.print_lol(other, fn = other_out) except fileError as err:
print('file error' + str(err)) 

man.out

this is the right room for an argument.
No you haven't
When?
No you didn't
You didn't

other.out

I've told you once.
Yes, I have.
Just now.
Yes I did.
I'm telling you, I did!

[Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out的更多相关文章

  1. cvs update后输出的文件标志 和 update常用的几个参数

    (1)update 和 checkout 在执行中,会为每个文件打印一行提示信息,文件的状态通过前面的单个字符指明:       U file        文件按要求从仓库得到更新.用在那些仓库里面 ...

  2. C 数组排序后输出至文件

    如题 C实现 #include<stdio.h> #define COUNT 9 //数组长度+1 #define FILE_NAME "data.txt" //文件名 ...

  3. log4j配置输出日志文件

    在测试程序时,有时候运行一次可能需要很久,把日志文件保存下来是很有必要的,本文给出了scala程序输出日志文件的方式,同时使用本人的另一篇博客中介绍的将log4j.properties放到程序jar包 ...

  4. python学习——读取染色体长度(六:读取含有染色体长度的文件)

    含有染色体长的文件chr_len.txt chr1 10chr2 20chr3 30chr4 40chr5 50 python脚本 #传递命令行参数 import sys # 导入模块 # 从命令行获 ...

  5. python之读取文件的测试数据

    假设我们有一个叫testdata.txt的文件,现在在这个文件里面有测试数据,我们怎么利用前2小章学习的知识,读取测试数据呢? 测试数据如下: url:https://www.cnblogs.com/ ...

  6. 【python】读取和输出到txt

    读取txt的数据和把数据保存到txt中是经常要用到的,下面我就总结一下. 读txt文件python常用的读取文件函数有三种read().readline().readlines() 以读取上述txt为 ...

  7. Python花式读取大文件(10g/50g/1t)遇到的性能问题(面试向)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_97 最近无论是面试还是笔试,有一个高频问题始终阴魂不散,那就是给一个大文件,至少超过10g,在内存有限的情况下(低于2g),该以什 ...

  8. js读取修改创建txt文本类型文件(.ini)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【ASP.NET 进阶】定时执行任务实现 (定时读取和修改txt文件数字内容,无刷新显示结果)

    现在有很多网站或系统需要在服务端定时做某件事情,如每天早上8点半清理数据库中的无效数据等等,Demo 具体实现步骤如下: 0.先看解决方案截图 1.创建ASP.NET项目TimedTask,然后新建一 ...

随机推荐

  1. js中的console

    console.log 是我们在调试代码的时候经常用到的一个方法,也可能也是很多人用的关于console的唯一方法,其实console对象下有很多不错的方法,现在记录总结于此.   log.info. ...

  2. 基于cygwin构建u-boot(四)libgcc引用

    接上文,config.mk文件修改后,即使没有.depend也可以正常处理了: 六.错误:gcclib引用错误 完成之前几篇的工作后,程序就可以一直执行了,直到最后生成u-boot, 出现如下错误告警 ...

  3. 实现android apk反编译后代码混淆

    通常情况下我们需要对我们开发的android代码进行混淆,以免代码在反编译时暴露敏感信息及相关技术代码: 反编译测试工具:onekey-decompile-apk-1.0.1. 在高级版本的adt创建 ...

  4. Android添加横线和竖线分割界面

    竖线 <View       android:layout_width="1dip"     android:layout_height="match_parent ...

  5. Google的Protocol Buffer格式分析

    [转]转自:序列化笔记之一:Google的Protocol Buffer格式分析 从公开介绍来看,ProtocolBuffer(PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.作 ...

  6. 高精度快速幂(Java版)

    import java.io.*; import java.math.*; import java.util.*; import java.text.*; public class Main { pu ...

  7. Top 15 Tools To Make Animated GIFs From Images & Video

    Creating an animated GIF picture from photos or video with Adobe Photoshop is easy, but not everyone ...

  8. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  9. LabVIEW新手5大错误

    虽然NI LabVIEW软件长期以来一直帮助工程师和科学家们快速开发功能测量和控制应用,但不是所有的新用户都会遵循LabVIEW编程的最佳方法. LabVIEW图形化编程比较独特,因为只需看一眼用户的 ...

  10. NET中级课--文件,流,序列化3

    1.序列化:将对象及状态保存起来. 反序列化就是逆操作. 2.NET提供了一个接口:System.runtime.serialization.IFormatter接口, 还有实现了这个接口的类Bina ...