python--io
- import io
- # io模块里面主要使用StringIo和BytesIo
- # StringIo
- f = io.StringIO() # 像使用open函数创建文件一样,生成一个句柄
- # 可以直接向f里面写入数据
- f.write("when i was young ,i'd listen to the radio\n")
- f.write("我是你爸爸\n")
- f.write("hello,mother fucker")
- # 获取内容可以通过getvalue函数
- data = f.getvalue()
- print(data)
- print(type(data))
- # 运行结果
- '''
- when i was young ,i'd listen to the radio
- 我是你爸爸
- hello,mother fucker
- <class 'str'>
- '''
- # 也可以在创建句柄的时候,直接写入数据
- f1 = io.StringIO("六月的古明地盆")
- data = f1.read() # 但此时要用f.read()函数
- print(data)
- print(type(data))
- # 运行结果
- '''
- 六月的古明地盆
- <class 'str'>
- '''
- # StringIo,由名字也可以看出,只能写入字符串,如果要写入字节类型,需要使用BytesIo
- # BytesIo的用法和StringIo的用法基本一样,只是写入内容的形式不一样,前者为字节,后者为字符串
- f = io.BytesIO()
- f.write(bytes("我是你爸 ", encoding="utf-8"))
- f.write(bytes("the feelings i have towards you is not lust,but limerence", encoding="utf-8"))
- data = f.getvalue()
- print(data)
- print(type(data))
- # 运行结果
- '''
- b'\xe6\x88\x91\xe6\x98\xaf\xe4\xbd\xa0\xe7\x88\xb8 the feelings i have towards you is not lust,but limerence'
- <class 'bytes'>
- '''
- # 使用BytesIo的时候,可以和StringIo一样在创建句柄的时候,写入数据
- f = io.BytesIO(bytes("轩墨是我身下受", encoding="utf-8"))
- data = f.read()
- print(data)
- print(type(data))
- # 运行结果
- '''
- b'\xe8\xbd\xa9\xe5\xa2\xa8\xe6\x98\xaf\xe6\x88\x91\xe8\xba\xab\xe4\xb8\x8b\xe5\x8f\x97'
- <class 'bytes'>
- '''
python--io的更多相关文章
- Python——IO多路复用之select模块epoll方法
Python——IO多路复用之select模块epoll方法 使用epoll方法实现IO多路复用,使用方法基本与poll方法一致,epoll效率要高于select和poll. .├── epoll_c ...
- Python——IO多路复用之select模块poll方法
Python——IO多路复用之select模块poll方法 使用poll方法实现IO多路复用 .├── poll_client.py├── poll_server.py└── settings.py ...
- Python——IO多路复用之select模块select方法
Python——IO多路复用之select模块select方法 使用select模块的select方法实现Python——IO多路复用 实现同时将终端输入的文本以及客户端传输的文本写入文本文件中: w ...
- python IO流操作
python IO流操作 学习完本篇,你将会独立完成 实现操作系统中文件及文件目录的拷贝功能. 将目标图片拷贝到指定的目录中 实现一个自动阅卷程序, Right.txt保存正确答案,xx(学生姓名). ...
- Python IO编程
IO在计算机中指Input/Output,也就是输入和输出 一.文件读写 1.读文件 >>> f = open('/Users/michael/test.txt', 'r') --- ...
- [Python]IO密集型任务 VS 计算密集型任务
所谓IO密集型任务,是指磁盘IO.网络IO占主要的任务,计算量很小.比如请求网页.读写文件等.当然我们在Python中可以利用sleep达到IO密集型任务的目的. 所谓计算密集型任务,是指CPU计算占 ...
- [Python] io 模块之 open() 方法
io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) 打开file ...
- python io 模块之 open() 方法(好久没写博客了)
io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True),打开file ...
- python IO 文件读写
IO 由于CPU和内存的速度远远高于外设的速度,所以,在IO编程中,就存在速度严重不匹配的问题. 如要把100M的数据写入磁盘,CPU输出100M的数据只需要0.01秒,可是磁盘要接收这100M数据可 ...
- Python IO 多路复用 \协程
IO 多路复用 作用: 检测多个socket是否已经发生变化(是否已经连接成功/是否已经获取数据) 即(可读/可写) IO请求时 解决并发 : 单线程 def get_data(key): cl ...
随机推荐
- Python locale 多语言模块和我遇到的坑
Table of Contents 1. locale遇到的问题 1.1. locale 简介 1.1.1. 什么是locale 1.1.2. locale 相关命令 1.2. Python loca ...
- java练习——多态与异常处理
1. 上面的程序运行结果是什么? 2. 你如何解释会得到这样的输出? parent = chlid; 所以child中的方法被赋予parent,所以用child方法输出了child的成员变量: ...
- 2 semantic ui 框架的应用
为什么使用css框架 1.使用基础样式 : ui segment 分段:内容片段 <link rel="stylesheet" href="css/semanti ...
- svn Previous operation has not finished; run 'cleanup' if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...
- MySQL数据库基础总结
来源: 实验楼 链接: https://www.shiyanlou.com/courses/9 一.开发准备 # 打开 MySQL 服务 sudo service mysql start #使用 ro ...
- dealloc时取weakself引起崩溃
今天无意这中遇到一个奇怪的崩溃,先上引起崩溃的代码: - (void)dealloc { __weak __typeof(self)weak_self = self; NSLog(@"%@& ...
- b树的实现
花了蛮长时间实现的b树插入操作.有时间再实现其他操作. #include <stdio.h> #include <stdlib.h> #define M 5 enum KeyS ...
- linux下多线程断点下载工具-axel
今天要下载一下14G左右的文件,用wget约10小时,后来发现linux下有个多线程支持断点续传的下载工具axel,试了一下,下载速度大大增加. 包地址:http://pkgs.repoforge.o ...
- Python 模块:random 随机数生成
Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...
- unity生命周期
1.静态构造函数 当程序集被加载的时候就被调用了,如果你的unity处于编辑状态时,此时你保存一个脚本(从而迫使重新编译),静态构造函数会立即被调用,因为unity加载了DLL.并且它将不会再次运行, ...