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返回当然作用域中的变量集合

 try:
data = open ('missing')
print(data.read_line(),end = '')
except IOError as err:
print('file error' + str(err))
finally:
if 'data' in locals():
data.close()
 try:
with open ('missing.txt','w') as data:
print("this is test",file = data)
except IOError as err:
print('file error' + str(err))

6- in 操作符用来检查成员关系

7- + 连接两个字符串

8- with 会自动处理所有已打开文件的关闭工作,即使出现异常也不例外, with也使用as关键字

9- sys.stdout 是python中"标准输出", 可以从标准库的sys模块访问

10- 标准库的pickle模块, 将python数据对象保存到磁盘及从磁盘恢复

11- pickle.dump() 函数将数据保存到磁盘

 try:
with open('man.out', 'wb') as man_out, open('other.out','wb') as other_out:
pickle.dump(man, man_out)
pickle.dump(other, other_out)

12- pickle.load() 函数从磁盘恢复数据

 new_man = []
try:
with open('man.out', 'rb') as man_file:
new_man = pickle.load(man_file) 

[Head First Python]4. summary的更多相关文章

  1. [Head First Python]6. summary

    1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...

  2. Python Syntax Summary

    # _*_ coding: utf-8 _*_ """########################################################## ...

  3. [Head First Python]5. summary

    1- "原地"排序-转换后替换 >>> list = [2,1,3] >>> list.sort() >>> list [1, ...

  4. Python初体验

    今天开始所有的工作脚本全都从perl转变到python,开发速度明显降低了不少,相信以后随着熟练度提升会好起来.贴一下今天一个工作代码,由于之前去一家小公司测序时,序列长度竟然都没有达到要求,为了之后 ...

  5. C#调用Python脚本打印pdf文件

     介绍:通过pdf地址先将文件下载到本地,然后调用打印机打印,最后将下载的文件删除. 环境:windows系统.(windows64位) windows系统中安装python3.6.2环境 资料: O ...

  6. 05基于python玩转人工智能最火框架之TensorFlow基础知识

    从helloworld开始 mkdir mooc # 新建一个mooc文件夹 cd mooc mkdir 1.helloworld # 新建一个helloworld文件夹 cd 1.helloworl ...

  7. Python实例--C#执行Python脚本,传参

    # -*- coding: utf-8 -*- # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错) # 这个 Python 脚本, 用于被 C# 来调用. # 简单测试 He ...

  8. Cheatsheet: 2013 09.22 ~ 09.30

    Other Python basics summary Another article about big O notation Mobile Getting Started with PhoneGa ...

  9. TensorFlow应用实战 | TensorFlow基础知识

    挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...

随机推荐

  1. (转)Android获取手机信息

    package com.water.activity; import java.util.List; import android.app.Activity; import android.os.Bu ...

  2. LeetCode_Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. Cmake中的find_package功能

    find_package其实在windows下扮演的角色并不是很重要.在Unix下就非常重要了,find_package可以根据cmake内置的.cmake的脚本去找相应的库的模块,当然,内建了很多库 ...

  4. JS 实现 startWith endWith函数

    String.prototype.startWith = function(s) { if (s == null || s == "" || this.length == 0 || ...

  5. 安全:加固你的ssh 登录

    SSH 是我们控制虚拟主机的一种途径,这个途径可以让我们拥有完全的控制权,如果对于这个控制权没有进行很好的安全处理,那么将会造成很大的安全问题.      我们可以在系统的日志文件 (例如:/var/ ...

  6. 方案:在Eclipse IDE 中搭建Python开发环境

    Eclipse是一款功能强大的IDE,Python是一种功能强大的计算机语言,但是Python的IDE环境确实很缺乏,如果在强大的Eclipse中添加Python开发环境,那样就很完美了. 在这里,我 ...

  7. Spring集成Hessian

    一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...

  8. 利用智能手机(Android)追踪一块磁铁(二)

    在上一篇博客中提到了利用磁场强度推算传感器位置坐标的公式,下面就介绍怎么利用智能手机完成磁铁的追踪(任何具有磁感应器的装置均可以),这里主要是利用Android手机. 1:程序步骤: 首先将磁铁放置在 ...

  9. Linux系统启动流程及grub重建(1)

    日志系统 Linux系统启动流程 PC: OS(Linux) POST-->BIOS(Boot Sequence)-->MBR(bootloader,446)-->Kernel--& ...

  10. 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4164  Solved: 1888[Submit] ...