![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
cookbook全书笔记
文章平均质量分 88
*z
这个作者很懒,什么都没留下…
展开
-
Cookbook:5.文件和IO
1.读写文本数据 对文本数据进行读写操作,可能涉及到多种编码 解决:用 open() 配合 rt 模式 with open('somefile.txt','rt') as f: data=f.read() 读写操作 open( ) wt 模式 wt 模式:如果文件已存在,会清除原先内容 书 P144 2.将输出重定向到文件 将print函数加上file参数即可: with open('somefile.text','rt') as f: print('ssss',file=f)原创 2021-04-08 14:52:46 · 141 阅读 · 0 评论 -
Cookbook:4.迭代器和生成器
0.迭代器和生成器 https://www.runoob.com/python3/python3-iterator-generator.html 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。 迭代器有两个基本的方法:iter() 和 next()。 字符串,列表或元组对象都可用于创建迭代器: >> list=[1,2,3,4] >>> it = iter(list) # 创建迭代器对象 >>> print(ne原创 2021-04-08 14:52:03 · 121 阅读 · 0 评论 -
Cookbook:3.数字日期和时间
3.1数值取整 将浮点数取固定的小数位 round(value,ndigits) round(1.23,1) #1.2 round(-1.27,1) #-1.3 round(1.27,1) #1.3 某个值为整数的一半时,取到离该值最近的偶数,即1.5和2.5都取为2 ndigits可以是负数 a=1627731 round(a,-1) #1637730 round(a,-3) #1628000 如果只是将数值以固定的位数输出,使用format x=1.23456 format(x,'0.2f') #'原创 2021-04-08 14:51:35 · 107 阅读 · 0 评论 -
Cookbook:2.字符串和文本
2.字符串和文本 2.1针对任意多的分割符拆分字符串 re.split()方法 line='asasd sda; asdw, sad ,sdaw, sdppppp' import re re.split(r'[;,\s]\s*',line) 2.2在字符串开头或结尾做文本匹配 str.startswith()和endswith() filename='spam.txt' filename.endswith('.txt') True 多项检查 import os filenames=os.li原创 2021-04-08 14:50:09 · 88 阅读 · 0 评论 -
cookbook:1.python数据结构
1.数据结构 1.8字典的计算 prices={ "ACME":45, "AAPL":23, "INM":22 } zip()函数 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。 zip 语法: zip([iterable, ...]) 参数明: iterabl – 一个或多个迭代器; 返回值 返回元组列原创 2021-04-08 14:50:47 · 141 阅读 · 1 评论