python内置模块(三)
hashlib模块
通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)。
Python2中使用hashlib:
import hashlib
m = hashlib.md5()
# m <md5 HASH object @ 0x0000000001E5C800>
src = "ling"
m.update(src)
print(m.hexdigest())
# 24c10be286b009f797d53126790fcfd8
Python3中使用hashlib:
import hashlib
m = hashlib.md5()
# m = hashlib.md5("123".encode("utf-8")) # 加入一个随机数
# m <md5 HASH object @ 0x0000000001E5C800>
src = bytes("ling",encoding="utf-8")
src1 = bytes("zhangsan",encoding="utf-8")
m.update(src)
m.update(src1)
print(m.hexdigest())
如果数据量很大,可以分块多次调用update()。
StringIO模块
有时候数据读写不一定是文件,也可以在内存中读写。StringIO就是在内存中读写str。
from io import StringIO
# StringIO只能存字符串 stringIO = StringIO()
stringIO.write("hello,world\n")
stringIO.write("hello,python")
print(stringIO.getvalue())
#hello,world
#hello,python
stringIO.truncate(0) # 清空所有写入的内容
stringIO.flush() # 刷新内部缓冲区
print(stringIO.getvalue())
#没有输出,内容已经被清空了
StringIO也可以像读取文件一样读取:
from io import StringIO
stringIO = StringIO("hello\nworld")
while True:
s = stringIO.readline()
if s == "":
break
print(s.strip())
BytesIO模块
from io import BytesIO
bytesIO = BytesIO()
bytesIO.write("中文".encode("utf-8"))
print(bytesIO.getvalue()) # 读取内容
from io import BytesIO
bytesIO = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')
bytesIO.read()
b'\xe4\xb8\xad\xe6\x96\x87'
Json模块
import json
test = '''[{"a":1, "aa":11, "aaa":111},{"b":2, "bb":22, "bbb":333}]'''
print(type(test)) # <type 'str'>
newTest = json.loads(test) # 把字符串转换成python对象
print(type(newTest)) # <type 'list'>
print(newTest[0]["a"]) #
针对python2乱码问题,使用json解决:
import json
a = dict(hello="你好")
print(a) # {'hello': '\xe4\xbd\xa0\xe5\xa5\xbd'}
print(a["hello"]) # 你好
print(json.dumps(a,ensure_ascii=False)) # 把python对象转换成字符串
# {"hello": "你好"}
对文件进行操作,文件和python对象相互转换:
import json
test = {"a":1, "b":2}
with codecs.open("1.txt","w") as f:
json.dump(test, f) # 把python对象写入文件
with codecs.open("1.txt","r") as f:
aa = json.load(f) # 把文件转换成python对象,aa是unicode类型
print(aa) # {u'a': 1, u'b': 2}
print(type(aa)) # <type 'dict'>
python内置模块(三)的更多相关文章
- python内置模块(4)
这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...
- python内置模块[sys,os,os.path,stat]
python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...
- python内置模块(time模块)
常用的python内置模块 一.time模块 在python的三种时间表现形式: 1.时间戳,给电脑看的. - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒. impor ...
- python 内置模块续(二)
目录 python 内置模块补充 1.hashlib模块 简易使用: 高级使用: 进阶使用: 加盐处理: 校验文件一致性 2.logging日志模块 日志等级 常用处理 "四大天王" ...
- Python内置模块(re+collections+time等模块)
Python内置模块(re+collections+time等模块) 1. re模块 import re 在python要想使用正则必须借助于模块 re就是其中之一 1.1 findall功能( re ...
- 学习Python的三种境界
前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...
- selenium webdriver (python) 第三版
感谢 感谢购买第二版的同学,谢谢你们对本人劳动成果的支持!也正是你们时常问我还出不出第三版了,也是你们的鼓励,让我继续学习整理本文档. 感谢乙醇前辈,第二版的文档是放在他的淘宝网站上卖的,感谢他的帮忙 ...
- Python第三天 序列 数据类型 数值 字符串 列表 元组 字典
Python第三天 序列 数据类型 数值 字符串 列表 元组 字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...
- 简学Python第三章__函数式编程、递归、内置函数
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- 初学Python(三)——字典
初学Python(三)——字典 初学Python,主要整理一些学习到的知识点,这次是字典. #-*- coding:utf-8 -*- d = {1:"name",2:" ...
随机推荐
- 从零开始学JAVA(08)-使用SpringMVC4 Restful 风格引用静态文件 css/js/png
在写完helloworld后想给网页加点样式(*.css),结果怎么也显示不了,百度了很多种方法后试行尝试,试验成功并记录下来,方便以后查看. 时隔两年,继续学习JAVA,太久没学了,忘记得差不多,还 ...
- 很实用的web性能测试插件:Yslow , PageSpeed
package org.springframework.web.servlet.resource; import java.io.IOException; import java.io.Unsuppo ...
- OpenStack概述
OpenStack OpenStack is a cloud operating system that controls large pools of compute, storage, and n ...
- Firebird 表字段查询
select rdb$relation_fields.rdb$relation_name table_name, rdb$relations.rdb$description table_des, rd ...
- 解读MySQL的慢日志
完整的慢日志格式一般如下: # Time: :: # User@Host: db_user[db_database] @ localhost [] # Query_time: Rows_examine ...
- amazeui笔记-CSS 布局相关
CSS 等分网格: 说明:.am-avg-sm-2 数字表示几等分 会将子元素 <li>的宽度设置为 50%. 只能用于 <ul> / <ol> 结构 辅助类: ...
- xcode开启后,每次调试运行要输入密码
1.contorl+空格 打开终端 2.输入DevToolsSecurity --status查看状态,如果是Developer mode is currently disabled.那就对了 3.输 ...
- 将字符串 “ hello word,你 好 世 界 ! ” 两端空格去掉并且将其中的其他所有空格替换成一个空格 输出结果为“hello word,你 好 世界”
string str = " hello word,你 好 世 界 ! "; string msg = str.Trim(); //去掉首尾空格 //使用split分割字符串,st ...
- [javaEE] response实现图片下载
在Servlet中的doGet()方法中 获取FileInputStream对象,new出来,构造参数:String的文件路径 得到文件路径,调用this.getServletContext().ge ...
- SpringMVC笔记:annotation注解式开发
一.配置式开发 在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学 ...