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. ZOJ 3818 Pretty Poem

    暴力模拟 细节处理很重要... #include <iostream> #include <cstring> #include <cstdio> using nam ...

  2. 学习ReactNative笔记整理一___JavaScript基础

    学习ReactNative笔记整理一___JavaScript基础 ★★★笔记时间- 2017-1-9 ★★★ 前言: 现在跨平台是一个趋势,这样可以减少开发和维护的成本.第一次看是看的ReactNa ...

  3. [Powershell] FTP Download File

    # Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...

  4. yum 配置详解(转发)

    一.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...

  5. 【POJ1581】A Contesting Decision(简单模拟)

    没有什么弯路,直接模拟即可.水题. #include <iostream> #include <cstring> #include <cstdlib> #inclu ...

  6. WifiDog系统

    WifiDog:A captive portal suite What is it composed of ? A: It is composed of 2 components: The clien ...

  7. 关于Tcp三次握手的思考

    一.为什么不能使两次握手,两次握手就应该可以保证线路的畅通? 1) 只能建立一个方向的连接,称为半连接 记住TCP是全双工的. A向B发出请求,同时收到B的确认,这时只有A.B知道A到B的连接成功了. ...

  8. WPF-24:绘制正多边形

    一般来说绘制正N边形,使用Blend直接画出来就好.不过可能是博主受WInform影响比较大,比较喜欢使用画出来的图形.如果要绘制正N边形,前面的绘制五角星的公式可以通用的(http://blog.c ...

  9. MVC Razor视图引擎

    Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...

  10. iOS开发之Crash分析,以及收集

    一  先谈谈iOS的Crash收集方式: 1. APP 发生crash,用户手机手机上肯定会有crash纪录,当然删除了该app,或是删了再装 crash纪录还是没了. 2. 如果用户设置-隐私  同 ...