新余百度网站建设,移动端的网站,企业简介模板word,node.js下载wordpress文章目录 一、文件读写1. 读文件2. 二进制文件3. 字符编码4. 写文件 二、StringIO和BytesIO三、操作文件和目录1. 操作系统命令2. 操作文件 四、序列化五、 JSON六、异步IO1. 协程2. asyncioasync/awaitaiohttp 一、文件读写
Python内置了读写文件的函数#xff0c;用法和C是… 文章目录 一、文件读写1. 读文件2. 二进制文件3. 字符编码4. 写文件 二、StringIO和BytesIO三、操作文件和目录1. 操作系统命令2. 操作文件 四、序列化五、 JSON六、异步IO1. 协程2. asyncioasync/awaitaiohttp 一、文件读写
Python内置了读写文件的函数用法和C是兼容的。
1. 读文件
使用Python内置的open()函数传入文件名和标示符 f open(/Users/michael/test.txt, r)标示符’r’表示读这样我们就成功地打开了一个文件。如果文件不存在open()函数就会抛出一个IOError的错误.
调用read()方法可以一次读取文件的全部内容 f.read()
Hello, world!调用close()方法关闭文件 f.close()Python引入了with语句来自动帮我们调用close()方法
with open(/path/to/file, r) as f:print(f.read())调用read()会一次性读取文件的全部内容 调用read(size)方法每次最多读取size个字节的内容 调用readline()可以每次读取一行内容 调用readlines()一次读取所有内容并按行返回list 2. 二进制文件
要读取二进制文件比如图片、视频等等用’rb’模式打开文件 f open(/Users/michael/test.jpg, rb)f.read()
b\xff\xd8\xff\xe1\x00\x18Exif\x00\x00... # 十六进制表示的字节3. 字符编码
读取非UTF-8编码的文本文件需要给open()函数传入encoding参数 f open(/Users/michael/gbk.txt, r, encodinggbk)f.read()
测试遇到编码错误忽略 f open(/Users/michael/gbk.txt, r, encodinggbk, errorsignore)4. 写文件
写文件和读文件是一样的唯一区别是调用open()函数时传入标识符’w’或者’wb’表示写文本文件或写二进制文件 with open(/Users/michael/test.txt, w) as f:f.write(Hello, world!)f.close()二、StringIO和BytesIO
StringIO 在内存中读写str from io import StringIOf StringIO()f.write(hello)
5f.write( )
1f.write(world!)
6print(f.getvalue())
hello world!BytesIO 操作二进制数据 from io import BytesIOf BytesIO()f.write(中文.encode(utf-8))
6print(f.getvalue())
b\xe4\xb8\xad\xe6\x96\x87三、操作文件和目录
1. 操作系统命令 import osos.name # 操作系统类型
posix如果是posix说明系统是Linux、Unix或Mac OS X如果是nt就是Windows系统。
在操作系统中定义的环境变量全部保存在os.environ这个变量中
2. 操作文件
# 查看当前目录的绝对路径:os.path.abspath(.)
/Users/michael
# 在某个目录下创建一个新目录首先把新目录的完整路径表示出来:os.path.join(/Users/michael, testdir)
/Users/michael/testdir
# 然后创建一个目录:os.mkdir(/Users/michael/testdir)
# 删掉一个目录:os.rmdir(/Users/michael/testdir)
# 对文件重命名:os.rename(test.txt, test.py)
# 删掉文件:os.remove(test.py)把两个路径合成一个时不要直接拼字符串而要通过os.path.join()函数
part-1/part-2要拆分路径时也不要直接去拆字符串而要通过os.path.split()函数 os.path.splitext(/path/to/file.txt)
(/path/to/file, .txt)四、序列化
Python提供了pickle模块来实现序列化。
序列化 import pickled dict(nameBob, age20, score88)pickle.dumps(d)
b\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x14X\x05\x00\x00\x00scoreq\x02KXX\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.pickle.dump()直接把对象序列化后写入一个file-like Object f open(dump.txt, wb)pickle.dump(d, f)f.close()反序列化 要把对象从磁盘读到内存时可以先把内容读到一个bytes然后用pickle.loads()方法反序列化出对象也可以直接用pickle.load()方法从一个file-like Object中直接反序列化出对象。 f open(dump.txt, rb)d pickle.load(f)f.close()d
{age: 20, score: 88, name: Bob}五、 JSON
JSON和Python内置的数据类型对应
JSON类型Python类型{}dict[]list“string”str1234.56int或floattrue/falseTrue/FalsenullNone
Python对象到JSON格式的转换 import jsond dict(nameBob, age20, score88)json.dumps(d)
{age: 20, score: 88, name: Bob}JSON反序列化为Python对象 json_str {age: 20, score: 88, name: Bob}json.loads(json_str)
{age: 20, score: 88, name: Bob}类序列化 需要为Student专门写一个转换函数再把函数传进去
def student2dict(std):return {name: std.name,age: std.age,score: std.score}import jsonclass Student(object):def __init__(self, name, age, score):self.name nameself.age ageself.score scores Student(Bob, 20, 88)
print(json.dumps(s, defaultstudent2dict))
{age: 20, name: Bob, score: 88}六、异步IO
1. 协程
协程又称微线程纤程。英文名Coroutine。协程的特点在于是一个线程执行 多线程比协程有何优势
协程极高的执行效率。因为子程序切换不是线程切换而是由程序自身控制因此没有线程切换的开销和多线程比线程数量越多协程的性能优势就越明显。不需要多线程的锁机制因为只有一个线程也不存在同时写变量冲突在协程中控制共享资源不加锁只需要判断状态就好了所以执行效率比多线程高很多。
Python对协程的支持是通过generator实现的。 协程生产者生产消息后直接通过yield跳转到消费者开始执行待消费者执行完毕后切换回生产者继续生产效率极高
def consumer():r while True:n yield rif not n:returnprint([CONSUMER] Consuming %s... % n)r 200 OKdef produce(c):c.send(None)n 0while n 5:n n 1print([PRODUCER] Producing %s... % n)r c.send(n)print([PRODUCER] Consumer return: %s % r)c.close()c consumer()
produce(c)结果
[PRODUCER] Producing 1...
[CONSUMER] Consuming 1...
[PRODUCER] Consumer return: 200 OK
[PRODUCER] Producing 2...
[CONSUMER] Consuming 2...
[PRODUCER] Consumer return: 200 OK
[PRODUCER] Producing 3...
[CONSUMER] Consuming 3...
[PRODUCER] Consumer return: 200 OK
[PRODUCER] Producing 4...
[CONSUMER] Consuming 4...
[PRODUCER] Consumer return: 200 OK
[PRODUCER] Producing 5...
[CONSUMER] Consuming 5...
[PRODUCER] Consumer return: 200 OK注意到consumer函数是一个generator把一个consumer传入produce后
首先调用c.send(None)启动生成器然后一旦生产了东西通过c.send(n)切换到consumer执行consumer通过yield拿到消息处理又通过yield把结果传回produce拿到consumer处理的结果继续生产下一条消息produce决定不生产了通过c.close()关闭consumer整个过程结束。
整个流程无锁由一个线程执行produce和consumer协作完成任务所以称为“协程”而非线程的抢占式多任务。
2. asyncio
用asyncio的异步网络连接来获取sina、sohu和163的网站首页
import asyncioasyncio.coroutine
def wget(host):print(wget %s... % host)connect asyncio.open_connection(host, 80)reader, writer yield from connectheader GET / HTTP/1.0\r\nHost: %s\r\n\r\n % hostwriter.write(header.encode(utf-8))yield from writer.drain()while True:line yield from reader.readline()if line b\r\n:breakprint(%s header %s % (host, line.decode(utf-8).rstrip()))# Ignore the body, close the socketwriter.close()loop asyncio.get_event_loop()
tasks [wget(host) for host in [www.sina.com.cn, www.sohu.com, www.163.com]]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()结果
wget www.sohu.com...
wget www.sina.com.cn...
wget www.163.com...
(等待一段时间)
(打印出sohu的header)
www.sohu.com header HTTP/1.1 200 OK
www.sohu.com header Content-Type: text/html
...
(打印出sina的header)
www.sina.com.cn header HTTP/1.1 200 OK
www.sina.com.cn header Date: Wed, 20 May 2015 04:56:33 GMT
...
(打印出163的header)
www.163.com header HTTP/1.0 302 Moved Temporarily
www.163.com header Server: Cdn Cache Server V2.0
...async/await
async和await是针对coroutine的新语法要使用新的语法只需要做两步简单的替换
把asyncio.coroutine替换为async 把yield from替换为await。
aiohttp
asyncio实现了TCP、UDP、SSL等协议aiohttp则是基于asyncio实现的HTTP框架。 安装aiohttp
pip3 install aiohttp然后编写一个HTTP服务器分别处理以下URL / - 首页返回b’Index /hello/{name} - 根据URL参数返回文本hello, %s!。
代码如下
import asynciofrom aiohttp import webasync def index(request):await asyncio.sleep(0.5)return web.Response(bodybh1Index/h1)async def hello(request):await asyncio.sleep(0.5)text h1hello, %s!/h1 % request.match_info[name]return web.Response(bodytext.encode(utf-8))async def init(loop):app web.Application(looploop)app.router.add_route(GET, /, index)app.router.add_route(GET, /hello/{name}, hello)srv await loop.create_server(app.make_handler(), 127.0.0.1, 8000)print(Server started at http://127.0.0.1:8000...)return srvloop asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()注意aiohttp的初始化函数init()也是一个coroutineloop.create_server()则利用asyncio创建TCP服务。