《python》学习笔记(Day2),读入磁盘文件,经过处理,保存成相应的磁盘文件
来源:互联网 发布:淘宝女主播九儿大魔王 编辑:程序博客网 时间:2024/04/30 14:41
一、print_lol函数。
"""This is the standard way to include a multiple-line comment in your code.这个模块,提供了一个名为print_lol的函数,这个函数的作用是打印列表,其中可能包含嵌套列表。"""
import sys
def print_lol(the_list,indent=False,level=0,fh=sys.stdout):
"""函数的第一个位置参数,可以是任何python列表。指定列表中的每个数据项会递归的输出在屏幕上,各数据项各占一行。第二个参数是默认情况下不打开缩进。第三个参数用于在遇到嵌套列表时插入制表符。第四个参数用来标识数据写入的位置,为一个文件对象。"""
for each_item in the_list:
if isinstance(each_item,list):
print_lol(each_item,indent,level+1,fh)
else:
if indent:
for tab_stop in range(level):
print>>fh,'\t',
print>>fh,each_item
二、磁盘文件处理代码。
import os
import nesteros.chdir('C:\\Python27\\HeadFirstPython\\chapter3')
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 DATA FILE IS MISSING!!'
try:
with open('man_data.txt','w')as man_file:
nester.print_lol(man,fh=man_file)
with open('other_data.txt','w')as other_file:
nester.print_lol(other,fh=other_file)
except IOError as err:
print 'File error:'+str(err)
0 0
- 《python》学习笔记(Day2),读入磁盘文件,经过处理,保存成相应的磁盘文件
- linux学习笔记(文件磁盘命令)
- 磁盘文件读入数组,处理后输入磁盘文件和屏幕
- 文件及磁盘处理
- Qt学习笔记--访问磁盘文件
- 磁盘文件的查找
- Qt5文件及磁盘处理
- 从磁盘中读入一个文件并写入另一个文件
- 数字图像处理--BMP文件的读入和保存
- 数字图像处理--BMP文件的读入和保存
- linux学习之磁盘文件
- 从键盘读入一行字符,把其中的字母字符一次存放在磁盘文件f2.dat中。再把它从磁盘文件读入程序,将其中的小写字母改为大写字母,再存入磁盘文件f3.dat
- PHP学习笔记——目录,文件,磁盘信息获取
- 【学习笔记——Linux】Linux磁盘和文件系统管理
- Linux学习笔记1:磁盘、文件与用户管理
- delphi中有关磁盘文件管理/处理的函数
- 记录处理磁盘镜像文件的单个分区
- wxWidget教程(6)--文件与磁盘处理
- sql语法大全
- Xcode 7智能测试化工具XCTestCase学习
- CentOS6.5下Mysql 未设置query_cache_size 导致CPU占用率高的问题解决方法_20160119_莫小贝
- MCMC and faster Gibbs Sampling using Rcpp
- CUDA编程——Mars:MapReduce on GPU
- 《python》学习笔记(Day2),读入磁盘文件,经过处理,保存成相应的磁盘文件
- linux下gcc编译conio.h问题
- 《MVC》——ViewData、ViewBag、TempData、model
- FLV封装原理
- 结合Phalcon看webapp中的MVC构架
- 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型
- 对IXP的一些思考
- iOS提交应用至APP Store流程
- 【Redis源码学习】检查RDB