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. Linux发行分支时间轴

    官网:http://futurist.se/gldt/ 全图png:http://futurist.se/gldt/wp-content/uploads/12.10/gldt1210.png 全图sv ...

  2. 通过Maven找java source源码方法

    在Maven的仓库中直接去下载 http://repo1.maven.org/maven2/ 如下:下载hibernate源码文件截图

  3. 基于Spring框架的Web应用开发笔记 - Outline

    Motivation 最近的工作涉及Web框架搭建,在了解公司原有采用框架基础上对Web开发技术栈做了一次升级,在次做记录. Audience J2EE Web Application Develop ...

  4. ZSDR101-跑成品MRP

    *&---------------------------------------------------------------------**& Report ZSDR101*&a ...

  5. HDU 5918 KMP/模拟

    Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. 使用npoi.dll导出数据到excel

    .net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...

  7. php归获取当前目录下的二级目录数 和文件数

    <?php        header('Content-Type: text/html; charset=gb2312');        // $baseDir = "/www/u ...

  8. poj2375 强连通

    题意:有一个 l * w 大小的滑雪场,每个格子都有一个高度,每个格子可以直接通到上下左右四个格子中高度小于等于自己的格子,现在要建立通道,能够连通任意两个格子,问最少建多少通道能够使所有格子能够互相 ...

  9. [转]ssh常用用法小结

    ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...

  10. Java 操作Excel 之Poi(第一讲)

    1.Poi 简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.HSSF - 提供读写Micros ...