Head_First_Python学习笔记(二)

来源:互联网 发布:七天网络微信怎么查分 编辑:程序博客网 时间:2024/05/17 07:05

使用range()和默认参数控制嵌套列表打印格式

#nester.pydef print_lol(the_list,level=0):    for item in the_list:            if isinstance(item,list):                    print_lol(item,level+1)            else:                    for tab_stop in range(level):                            print("\t"),                    print(item)

>>> import nester>>> movies = ['the holy grail', 1975, ['the life of brain', 1979,[ 'the meaning of life', 1983]]]>>> nester.print_lol(movies,1)    the holy grail    1975        the life of brain        1979            the meaning of life            1983
  • pytho3.x print不换行:print(“\t”,end=”)
  • pytho2.x print不换行:print(“\t”),(后面加逗号)

增加一个默认参数判断是否缩进

#nester.pydef print_lol(the_list,indend=False,level=0):    for item in the_list:            if isinstance(item,list):                    print_lol(item,indend,level+1)            else:                    if indend:                            for tab_stop in range(level):                                    print("\t"),                    print(item)

>>> nester.print_lol(movies,True)the holy grail1975    the life of brain    1979        the meaning of life        1983

文件读取

>>> import os>>> os.getcwd()'/Users/zhang/Documents/mou/python/nester'>>> data = open('夹边沟记事.txt')>>> print(data.readline()),《夹边沟记事》>>> print(data.readline()),正文>>> print(data.readline()),作者介绍>>> print(data.readline()),>>> print(data.readline()),>>> print(data.readline()),>>> >>> print(data.readline()),杨显惠,1946年出生于兰州。中国作家协会会员,现居天津。>>> print(data.readline()),>>> print(data.readline()),>>> print(data.readline()),>>> print(data.readline()),1965年由兰州二中上山下乡赴甘肃省生产建设兵团安西县小宛农场。1971年入甘肃师范大学数学系读书。1975年在甘肃省农垦局酒泉农垦中学做教师。1981年调往河北省大清河盐场工作。1988年入天津作家协会专职写作至今。主要作品收入《这一片大海滩》、《夹边沟记事》、《告别夹边沟》等书。短篇小说曾获全国短篇小说奖、中国小说学会奖、《上海文学》奖。>>> data.seek(0)>>> 

>>> for each_line in data:...     print each_line,... 《夹边沟记事》正文作者介绍......谢谢读者。2008年6月15日兰州 >>>data.close()

切分文本

>>> data = open('hello.txt')>>> for each_line in data:...     (role,line_spoken) = each_line.split(':')...     print role,...     print 'said:',...     print line_spoken,... hello said: worldhello said: worldhello said: world>>> 

写入文件

>>> out = open('data.txt','w')>>> print>>out,'hello,world'>>> out.close()

上面为py2的写法,py3使用print(‘hello,world’,file=out)

  • w:如文件已存在,则会清空现有内容
  • a:追加到一个文件
  • w+:打开一个文件完成读写,不清除现有内容

locals和globals

当一行代码要使用变量 x 的值时,Python会到所有可用的名字空间(每个函数及变量都有自己的命名空间,局部命名空间)去查找变量,按照如下顺序:

  • 1.局部名字空间 - 特指当前函数或类的方法。如果函数定义了一个局部变量 x,Python将使用这个变量,然后停止搜索。(locals BIF)
  • 2.全局名字空间 - 特指当前的模块。如果模块定义了一个名为 x 的变量,函数或类,Python将使用这个变量然后停止搜索。(globals BIF)
  • 3.内置名字空间 - 对每个模块都是全局的。作为最后的尝试,Python将假设 x 是内置函数或变量。

打印错误信息

except IOError as err:    print('File error:'+ str(err))

使用with处理文件

>>> try:...     with open('1234.txt') as data:...             print data.readline()... except IOError as err:...     print('File error' + str(err))... File error[Errno 2] No such file or directory: '1234.txt'

with利用了一种名为上下文管理协议(context management protocol)的Python技术,抽象了:

finally:    if 'data' in locals():        data.close()
0 0
原创粉丝点击