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的更多相关文章

  1. Python——IO多路复用之select模块epoll方法

    Python——IO多路复用之select模块epoll方法 使用epoll方法实现IO多路复用,使用方法基本与poll方法一致,epoll效率要高于select和poll. .├── epoll_c ...

  2. Python——IO多路复用之select模块poll方法

    Python——IO多路复用之select模块poll方法 使用poll方法实现IO多路复用 .├── poll_client.py├── poll_server.py└── settings.py ...

  3. Python——IO多路复用之select模块select方法

    Python——IO多路复用之select模块select方法 使用select模块的select方法实现Python——IO多路复用 实现同时将终端输入的文本以及客户端传输的文本写入文本文件中: w ...

  4. python IO流操作

    python IO流操作 学习完本篇,你将会独立完成 实现操作系统中文件及文件目录的拷贝功能. 将目标图片拷贝到指定的目录中 实现一个自动阅卷程序, Right.txt保存正确答案,xx(学生姓名). ...

  5. Python IO编程

    IO在计算机中指Input/Output,也就是输入和输出 一.文件读写 1.读文件 >>> f = open('/Users/michael/test.txt', 'r') --- ...

  6. [Python]IO密集型任务 VS 计算密集型任务

    所谓IO密集型任务,是指磁盘IO.网络IO占主要的任务,计算量很小.比如请求网页.读写文件等.当然我们在Python中可以利用sleep达到IO密集型任务的目的. 所谓计算密集型任务,是指CPU计算占 ...

  7. [Python] io 模块之 open() 方法

    io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) 打开file ...

  8. python io 模块之 open() 方法(好久没写博客了)

    io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True),打开file ...

  9. python IO 文件读写

    IO 由于CPU和内存的速度远远高于外设的速度,所以,在IO编程中,就存在速度严重不匹配的问题. 如要把100M的数据写入磁盘,CPU输出100M的数据只需要0.01秒,可是磁盘要接收这100M数据可 ...

  10. Python IO 多路复用 \协程

    IO 多路复用 作用:  检测多个socket是否已经发生变化(是否已经连接成功/是否已经获取数据) 即(可读/可写) IO请求时 解决并发  :  单线程 def get_data(key): cl ...

随机推荐

  1. TouTiao开源项目 分析笔记2

    1.Constant常量定义类 1.1.源代码 public class Constant { public static final String USER_AGENT_MOBILE = " ...

  2. Java语言基础---转义符

    转义符 转义符使用“\”表示.常用转义符如下: 1.‘\n’回车 2.‘\t’制表位字符,一个表示向右跳8-10个字符 3.‘\\’表示’\’ 4.‘\’’表示单引号 5.‘\’’’表示双引号 6.‘ ...

  3. 2286: [Sdoi2011]消耗战

    2286: [Sdoi2011]消耗战 链接 分析 虚树练习题. 构建虚树,在虚树上DP. 跟着gxb学虚-tree... 代码 #include <cstdio> #include &l ...

  4. MySQL之查询性能优化(三)

    MySQL查询优化器的局限性 MySQL的万能“嵌套循环”并不是对每种查询都是最优的.不过还好,MySQL查询优化只对少部分查询不适用,而且我们往往可以通过改写查询让MySQL高效地完成工作. 关联子 ...

  5. CF6C Alice, Bob and Chocolate

    CF6C Alice, Bob and Chocolate 题目链接 写了一天搜索写的有点累了,就顺手水了一道CF的模拟题 这道题就是简单的模拟整个题的过程,注意最后输出的形式就好了QWQ AC代码如 ...

  6. sqlsever存储过程学习笔记

    1,创建数据表 use test create table money( id ,) primary key, money int, monetary_unity char ); 2,考虑到货币单位的 ...

  7. leetcode 【 Remove Duplicates from Sorted List 】 python 实现

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  8. 【志银】#define lowbit(x) ((x)&(-x))原理详解

    分析下列语句 #define lowbit(x) ((x)&(-x)) 可写成下列形式: int Lowbit(x) { return x&(-x); } 例1:x = 1 十进制转二 ...

  9. Tensorflow实现LSTM识别MINIST

    import tensorflow as tf import numpy as np from tensorflow.contrib import rnn from tensorflow.exampl ...

  10. React02

    目录 React 进阶提升 条件渲染 受控组件* 状态提升* 组件数据流 TODO-LIST 设置服务器端口 列表渲染 条目PropTypes检查类型 export & import Refs ...