首页 > 代码库 > [Head First Python]4. summary
[Head First Python]4. summary
1- strip()方法可以从字符串去除不想要的空白符
(role, line_spoken) = each_line.split(":", 1)line_spoken = line_spoken.strip()
2- print() BIF的file参数控制将数据发送/保存到哪里
print(...) print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
if indent: for tab_stop in range(level): print("\t",end=‘‘, file = fn)print(each_item, file = fn)
3- 会向except组传入一个异常对象,并使用as关键字赋至一个标示符
except IOError as err: print(‘file error‘ + str(err))except pickle.PickleError as perr: print(‘pickle err‘ + str(perr))
4- str() BIF可以用来访问任何数据对象的串表示
5- locals() BIF返回当然作用域中的变量集合
1 try:2 data = http://www.mamicode.com/open (‘missing‘)3 print(data.read_line(),end = ‘‘)4 except IOError as err:5 print(‘file error‘ + str(err))6 finally:7 if ‘data‘ in locals():8 data.close()
1 try:2 with open (‘missing.txt‘,‘w‘) as data:3 print("this is test",file = data)4 except IOError as err:5 print(‘file error‘ + str(err))
6- in 操作符用来检查成员关系
7- + 连接两个字符串
8- with 会自动处理所有已打开文件的关闭工作,即使出现异常也不例外, with也使用as关键字
9- sys.stdout 是python中"标准输出", 可以从标准库的sys模块访问
10- 标准库的pickle模块, 将python数据对象保存到磁盘及从磁盘恢复
11- pickle.dump() 函数将数据保存到磁盘
1 try:2 with open(‘man.out‘, ‘wb‘) as man_out, open(‘other.out‘,‘wb‘) as other_out:3 pickle.dump(man, man_out) 4 pickle.dump(other, other_out)
12- pickle.load() 函数从磁盘恢复数据
1 new_man = []2 try:3 with open(‘man.out‘, ‘rb‘) as man_file:4 new_man = pickle.load(man_file)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。