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. 简单的thinkPHP3.2运行实例。

    在上一篇的环境基础下. 我们用zendstudio12.5版本编写我们的代码.具体的下载方式在这里就不多做注明了.自己百度就可以搞定. 首先我们用zendstudio12.5 导入我们从网上随处都可以 ...

  2. 如何从数据库(实体提供者)读取安全用户(转自http://wiki.jikexueyuan.com/project/symfony-cookbook/entity-provider.html)

    Symfony 的安全系统可以通过活动目录或开放授权服务器像数据库一样从任何地方加载安全用户.这篇文章将告诉你如何通过一个 Doctrine entity 从数据库加载用户信息. 前言 在开始之前,您 ...

  3. ibatis 更改resultmap后 java.sql.SQLException: Column 'del_status' not found.

    当在resultmap中增加字段后,查询语句也必须增加相应字段,否则会报错, java.sql.SQLException: Column 'del_status' not found. 因为查询结果与 ...

  4. python保留两位小数

    python保留两位小数: In [1]: a = 5.026 In [2]: b = 5.000 In [3]: round(a,2) Out[3]: 5.03 In [4]: round(b,2) ...

  5. Hdu1076(n个闰年后的年份)

    #include <stdio.h> #include<stdlib.h> int main() { int T,Y,n,printYear; scanf("%d&q ...

  6. FMDB官方使用文档-GCD的使用-提高性能(翻译)

    FMDB官方使用文档-GCD的使用-提高性能(翻译) 发布于:2013-08-19 10:01阅读数:13395 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此 ...

  7. 迁移笔记:php缓存技术memcached

    1)memcached 的几个指令: -p监听的端口 -l连接的IP地址, 默认是本机 -d start启动memcached服务 -d restart重起memcached服务 -d stop|sh ...

  8. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

  9. Quartus DSE 初步应用

    介绍 Design Space Explorer (DSE) is a program that automates the process of finding the optimal collec ...

  10. 认识什么是SEO

    何为SEO? SEO是由英 文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”,是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网 ...