1. 定义一个print_lol函数来控制列表的缩进和写入位置

import sys
"""this is a new fuction, which work for a list"""
def print_lol(the_list,indent=False,level=0,where=sys.stdout):
""" one arguement is the_list"""
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item,indent,level+1,where)
else:
if indent:
for tab_stop in range(level):
print("\t",end='',file=where)
print(each_item,file=where)

2. 将更新的代码安装到本地库

C:\Users\eric\Documents\Python\nester>c:\Users\eric\AppData\Local\Programs\Python\Python35-32\python.exe setup.py install
running install
running build
running build_py
copying nester.py -> build\lib
running install_lib
copying build\lib\nester.py -> c:\Users\eric\AppData\Local\Programs\Python\Python35-32\Lib\site-packages
byte-compiling c:\Users\eric\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\nester.py to nester.cpython-35.pyc
running install_egg_info
Removing c:\Users\eric\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\iamericnester-1.6.1-py3.5.egg-info
Writing c:\Users\eric\AppData\Local\Programs\Python\Python35-32\Lib\site-packages\iamericnester-1.6.1-py3.5.egg-info

3. 前段代码格式化文件内容并分割将内容存入man和other两个对象,后段代码以写模式打开2个新的文件并man和other对象存入其中。(存入的过程中调用print_lol进行格式再处理)

import nester
import sys
man = []
other = []
try:
data = open('sketch.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 ('The datafiel is missing!') try:
with open('man_data.txt','w') as man_file:
nester.print_lol(man,where=man_file) with open('other_data.txt','w') as other_file:
nester.print_lol(other,where=other_file) except IOError as err:
print('file error:' +str(err))

Python 通过print_lol将数据保存到文件中的更多相关文章

  1. Python 通过print将数据保存到文件中

    1. Print them to screen man = [] other = [] try: data = open('sketch.txt') for each_line in data: tr ...

  2. python将字典中的数据保存到文件中

    d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()

  3. Python3操作MySQL,查询数据并保存到文件中

    我们在测试过程中,可能需要到数据库中拉去一些数据,为从测试准备.比如最近在做接口性能测试的时候,就需要很多数据来支撑,所以就需要的数据库去查询数据,下面就是python3 查询 mysql 并且保存到 ...

  4. 『无为则无心』Python日志 — 66、将日志信息保存到文件中

    目录 1.把日志信息保存到文件中 2.拓展 (1)观察代码 (2)提出问题 (3)问题说明 1.把日志信息保存到文件中 代码如下所示: """ logging模块是Pyt ...

  5. php cUrl模拟登录,cookie保存到文件中

    源码如下: <?php header("Content-Type:text/html;charset=utf-8"); //模拟群友通讯录手机号登录 $curl = curl ...

  6. json和xml封装数据、数据缓存到文件中

    一.APP的通信格式之xml xml:扩展标记语言,可以用来标记数据,定义数据类型,是一种允许用户对自己标记语言进行定义的源语言.XML格式统一,扩平台语言,非常适合数据传输和通信,业界公认的标准. ...

  7. 利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中

    既然已经可以通过 RTSP 获取h264 裸流了.那么通过 FFmpeg 将其保存到文件中怎么做呢? 一.首先RTSP获取 h264 裸流 我们上面两篇文章主要讲的是通过 rtsp://Your ip ...

  8. Object 保存到文件中

    6月4日 Object 保存到文件中  Q. 你添加一个新类到你的项目当中且你希望可以保存这个类的一个实例对象到磁盘文件 并在需要时从磁盘文件读回到内存中  A. 方案  确保你的类遵循 NSCodi ...

  9. Java将对象保存到文件中/从文件中读取对象

    1.保存对象到文件中 Java语言只能将实现了Serializable接口的类的对象保存到文件中,利用如下方法即可: public static void writeObjectToFile(Obje ...

随机推荐

  1. CentOS 6.2 OpenVPN 搭建

    一.yum 安装. 二.下载 easy-rsa 3.0 三.生成根证书.服务器.客户端证书.ta 生成流程: wget -c https://github.com/OpenVPN/easy-rsa/a ...

  2. 高效而轻松的sed命令

    sed(stream editor)是一款高效的流编辑器,它一次只处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的 ...

  3. c 函数及指针学习 7

    1.结构的存储分配 1 2 printf("%d \n",sizeof(char)); printf("%d \n",sizeof(int)); int 类型为 ...

  4. 网站优化之-SEO在网页制作中的应用(信息来自慕课网课程笔记)

    一.SEO基本介绍. 1.搜索引擎工作原理. 2.seo简介:SEarch Engine Optimization,搜索引擎优化.为了提升网页在搜索引擎自然搜索结果中的收录数量及排序位置而做的优化行为 ...

  5. tulterbot遥感操作使用Interactive Markers--12

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.安装ros indigo功能包: sudo apt-get install ros-indigo-t ...

  6. 关于xmlhttprequest的readystate属性的五个状态

    http://www.cnblogs.com/jerry01/archive/2009/09/03/1559624.html 昨天做了一个利用 ajax实现页面无刷新的从服务器端获得时间的例子,当时对 ...

  7. 20145220&20145209&20145309信息安全系统设计基础实验报告(1)

    实验贡献:韩旭飞 刘一阳 李昊实验报告贡献:李昊 刘一阳 韩旭飞 PART1 一.实验原理 交叉编译,简单地说,就是在一个平台上生成另一个平台上的可执行代码.同一个体系结构可以运行不同的操作系统:同样 ...

  8. 代码备份:处理 SUN397 的代码,将其分为 80% 训练数据 以及 20% 的测试数据

    处理SUN397 的代码,将其分为80% 训练数据以及20% 的测试数据 2016-07-27 1 %% Code for Process SUN397 Scene Classification 2 ...

  9. Improve Scalability With New Thread Pool APIs

    Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...

  10. Oracle DBWR,LGWR,CKPT,ARCH 触发条件 总结

    一.  DBWR写磁盘数据触发条件 1. 当进程在辅助LRU链表和主LRU链表上扫描以查找可以覆盖的buffer header[空闲缓冲区]时,如果已经扫描的buffer header的数量到达一定的 ...