[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返回当然作用域中的变量集合
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的更多相关文章
- [Head First Python]6. summary
1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...
- Python Syntax Summary
# _*_ coding: utf-8 _*_ """########################################################## ...
- [Head First Python]5. summary
1- "原地"排序-转换后替换 >>> list = [2,1,3] >>> list.sort() >>> list [1, ...
- Python初体验
今天开始所有的工作脚本全都从perl转变到python,开发速度明显降低了不少,相信以后随着熟练度提升会好起来.贴一下今天一个工作代码,由于之前去一家小公司测序时,序列长度竟然都没有达到要求,为了之后 ...
- C#调用Python脚本打印pdf文件
介绍:通过pdf地址先将文件下载到本地,然后调用打印机打印,最后将下载的文件删除. 环境:windows系统.(windows64位) windows系统中安装python3.6.2环境 资料: O ...
- 05基于python玩转人工智能最火框架之TensorFlow基础知识
从helloworld开始 mkdir mooc # 新建一个mooc文件夹 cd mooc mkdir 1.helloworld # 新建一个helloworld文件夹 cd 1.helloworl ...
- Python实例--C#执行Python脚本,传参
# -*- coding: utf-8 -*- # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错) # 这个 Python 脚本, 用于被 C# 来调用. # 简单测试 He ...
- Cheatsheet: 2013 09.22 ~ 09.30
Other Python basics summary Another article about big O notation Mobile Getting Started with PhoneGa ...
- TensorFlow应用实战 | TensorFlow基础知识
挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...
随机推荐
- [linux] linux知识积累(不断更新中…)
一.vim知识 f(find)命令也可以用于移动,fx将找到光标后第一个为x的字符,3fd将找到第三个为d的字符. D 删除当前字符至行尾.D=d$ :split或new 打开一个新窗口,光标停在顶层 ...
- 灯塔(LightHouse)
Description As shown in the following figure, If another lighthouse is in gray area, they can beacon ...
- CC2530定时器1的模模式中断
CC2530定时器1的模模式中断void timer1SInit(void){ T1CCTL0 = 0; T1CTL &= ~0x0F; //clear register T1CTL |= 0 ...
- OC与Swift混编
群里大神发的网址,感觉有用就先收录了,暂时没时间看SWIFT,感觉代码简洁,但是可阅读性不是太高,有些代码让系统去判断类型,同样的,我们看代码的时候也得自己去判断类型,或许看多就习惯了,有时间再说吧, ...
- stm32的FSMC
之前用的stm32f103rbt6,它是100引脚以内的,不带FSMC.驱动液晶屏或者SRAM要自己写时序方面的程序,比较麻烦.后来换成stm32f103zet6,带有FSMC.不过在学习FSMC的时 ...
- Android provider authorities冲突
在Android项目中的AndroidManifest中provider的authorities相当于一个系统级的全局变量它的值必须唯一,如果有两个应用的authorities相同会导致安装失败.
- CART剪枝
与上篇文章中提到的ID3算法和C4.5算法类似,CART算法也是一种决策树分类算法.CART分类回归树算法的本质也是对数据进行分类的,最终数据的表现形式也是以树形的模式展现的,CART与ID3,C4. ...
- ORA-19573: cannot obtain exclusive enqueue for datafile 1
还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...
- android 登录界面
http://blog.csdn.net/jiabinjlu/article/details/6920967 http://blog.csdn.net/myserverthepeople/articl ...
- POJ 1379 Run Away
题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交 ...