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. php给一张图片加上水印效果

    <?php /** * 功能:给一张图片加上水印效果 * $i 要加水印效果的图片 * $t 水印文字 * $size 文字大小 * $pos 水印的位置 * $color 文字的颜色 * $f ...

  2. mac删除顽固图标

    cd  /Users/shelley/Library/Application\ Support/Dock cp  10CCA448-0975-41DE-B47A-8E89FD634227.db  10 ...

  3. Song of Pi

    def main(): pi = ' # 预先给出需要比较的值 t = int(raw_input()) for _ in xrange(t): song = raw_input().strip(). ...

  4. AT89C 系列单片机解密原理

    单片机解密简单就是擦除单片机片内的加密锁定位.由于AT89C系列单片机擦除操作时序设计上的不合理.使在擦除片内程序之前首先擦除加密锁定位成为可能.AT89C系列单片机擦除操作的时序为:擦除开始---- ...

  5. MVC 自定义错误处理

    1. Application_Error namespace Libaray.Web{ public class MvcApplication : System.Web.HttpApplication ...

  6. python 文件中的中文编码解决方法

    # -*- coding: utf-8 -*- #查看安装的SDK默认的编码字符集在脚本中可以修改你的编码格式, 方法如下:#sys.getdefaultencoding()#reload(sys)# ...

  7. Linux 日志

    成功地管理任何系统的关键之一,是要知道系统中正在发生什么事.Linux 中提供了异常日志,并且日志的细节是可配置的. Linux 日志都以明文形式存储,所以您不需要特殊的工具就可以搜索和阅读它们.您还 ...

  8. 关于google的C++ coding style

    大家都知道google的开源项目有很多,不过我观察过一些开源项目,觉得代码质量就是这家最好了.这些“教条”式规定的背后是是来自于常年工程经验积累上的理性思考. 为什么好?主要有以下几点: 1.规范,就 ...

  9. 通过Excel来集中管理资源文件

     在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具.借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划 ...

  10. SQL Server (MSSQLSERVER) 服务因 找不到指定的模块。 服务特定错误而停止。

    新装了sql server 2008,发现sqlserver 服务没法起来.查看系统日志是7024如下: 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 ...