Python文件操作,时间日期操作,collections增强,Deque(类似java的LinkedList),OrderedDict,Counter
2017-07-14 19:19
471 查看
1、文件操作的案例:
#文件操作的三种方式 #r = read 读取文件 #w = write 写入文件,覆盖掉 #a = append 追加写入,不会覆盖原有的内容 #读文件 f = open("E://wordcount//input//1.txt","r") data = f.read() print(data) #读文件 一行一行的读取 path = "E://wordcount//input//1.txt" f = open(path,"r") for line in f.readlines(): print("------------->" + line) ##覆盖写 path = "E://wordcount//input//1.txt" f = open(path,"w",encoding="utf-8") ##这种方式写的时候,会吧别人的覆盖了。 f.write("I love my country") f.close() ##追加写 path = "E://wordcount//input//2.txt" f = open(path,"a",encoding="utf-8") #写一个分割 f.write("\n") f.write("I love you two") f.close() #python中的try,catch #异常处理的格式 try: path = "E://wordcount//input//2.txt" f = open(path,"r",encoding="utf-8") for line in f.readlines(): print(line) finally: #不管怎样都关闭掉文件 f.close() ##异常处理的简单写法 with open(path,"r",encoding="utf-8") as f: for i in f.readlines(): print("xxxx" + i)
2、Python中的操作时间
将date类型转化成固定的字符串将字符串转化成date类型
#将date转换成字符串 #使用datetime需要导入一个类库,下面是导入类型对的方式,导包的方式还是选中 #datetime,然后Alt + Enter键,选择合适的包 from datetime import datetime,timedelta #获取当前时间 now = datetime.now() print(now) #将事件转成成有格式类型的时间 str_time = now.strftime("%Y-%m-%d %H:%M:%S") print(str_time) ##将字符串转为Date类型 date = datetime.strptime("20170714 19:27:51","%Y%m%d %H:%M:%S") print(date) #对日期进行修改 now = datetime.now() #在现有的基础上加8个小时 now = now + timedelta(hours=8) print(now) ##加3天 now = now + timedelta(days=3) print(now) #减3天 now = now + timedelta(days=-3) print(now) #减8小时 now = now + timedelta(hours=-8) print(now)
运行结果:
3.collections增强
namedtuple:能够tuple的每个元素起个名字基于tuple的扩展,tuple一旦被创建无法修改,获取tuple中的数据,只能通过角标值进行修改。
from collections import namedtuple tuple = ("name","age","sex") #通过角标值获取元素 tuple[0] #namedtuple,如果作为一个单独的数据结构,就是每个元素都有名称 #并且一旦创建,就无法修改。 Point = namedtuple("Point",["name","age","sex"]) p = Point("旺财","3","公") print(p.name)
运行结果:
Deque:
相当于java中的linkedlist,特征:查询慢,删除修改快。
from collections import deque #linked的定义 linked_list = deque(["1","2","3","4"]) #获取linkedlist的元素 和删除元素 item = linked_list.pop() print(linked_list) item = linked_list.popleft() print(item) print(linked_list) #添加元素,append默认在最后面添加4,appendLeft默认在最左边添加元素 linked_list.append("4") linked_list.appendleft("1") print(linked_list)
运行结果:
deque(['1', '2', '3']) 1 deque(['2', '3']) deque(['1', '2', '3', '4'])
4 OrderedDict
from collections import OrderedDict #orderdict 按照插入的顺序排序 map = OrderedDict([("key1",3),("key2",2),("key0",1)]) for item in map.items(): print(item)
运行结果:
('key1', 3) ('key2', 2) ('key0', 1)
from collections import Counter #计数器,计算每个字符出现的次数 #单词计算 counter = Counter() str = "dasetgerfa" for ch in str: counter[ch] = counter[ch] + 1 print(counter)
运行结果是:
Counter({'a': 2, 'e': 2, 'd': 1, 's': 1, 't': 1, 'g': 1, 'r': 1, 'f': 1})
上面实现的逻辑相当于是:
map = dict() str = "hello" for ch in str: value = map.get(ch) if value == None: map[ch] = 1 else: value += 1 map[ch] = value print(map)
相关文章推荐
- java文件操作,集合操作 时间日期的操作 bulidPath介绍
- paip.日期时间操作以及时间戳uapi php java python 总结
- paip.日期时间操作以及时间戳uapi php java python 总结
- Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter
- Java Collections Framework之Deque(LinkedList实现)源码分析(基于JDK1.6)(已补充)
- java中关于时间日期操作的常用函数
- 收藏:java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数(转摘)
- Java文件操作增强工具
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数(转摘)
- java中关于时间日期操作的常用函数
- java中关于时间日期操作的常用函数
- JAVA中的日期时间操作
- java中关于时间日期操作的常用函数
- java中关于时间日期操作地常用函数
- java中关于时间日期操作的常用函数