1. import io
  2.  
  3. # io模块里面主要使用StringIo和BytesIo
  4.  
  5. # StringIo
  6. f = io.StringIO() # 像使用open函数创建文件一样,生成一个句柄
  7. # 可以直接向f里面写入数据
  8. f.write("when i was young ,i'd listen to the radio\n")
  9. f.write("我是你爸爸\n")
  10. f.write("hello,mother fucker")
  11. # 获取内容可以通过getvalue函数
  12. data = f.getvalue()
  13. print(data)
  14. print(type(data))
  15. # 运行结果
  16. '''
  17. when i was young ,i'd listen to the radio
  18. 我是你爸爸
  19. hello,mother fucker
  20. <class 'str'>
  21. '''
  22.  
  23. # 也可以在创建句柄的时候,直接写入数据
  24. f1 = io.StringIO("六月的古明地盆")
  25. data = f1.read() # 但此时要用f.read()函数
  26. print(data)
  27. print(type(data))
  28. # 运行结果
  29. '''
  30. 六月的古明地盆
  31. <class 'str'>
  32. '''
  33.  
  34. # StringIo,由名字也可以看出,只能写入字符串,如果要写入字节类型,需要使用BytesIo
  35. # BytesIo的用法和StringIo的用法基本一样,只是写入内容的形式不一样,前者为字节,后者为字符串
  36.  
  37. f = io.BytesIO()
  38. f.write(bytes("我是你爸 ", encoding="utf-8"))
  39. f.write(bytes("the feelings i have towards you is not lust,but limerence", encoding="utf-8"))
  40. data = f.getvalue()
  41. print(data)
  42. print(type(data))
  43. # 运行结果
  44. '''
  45. b'\xe6\x88\x91\xe6\x98\xaf\xe4\xbd\xa0\xe7\x88\xb8 the feelings i have towards you is not lust,but limerence'
  46. <class 'bytes'>
  47. '''
  48.  
  49. # 使用BytesIo的时候,可以和StringIo一样在创建句柄的时候,写入数据
  50. f = io.BytesIO(bytes("轩墨是我身下受", encoding="utf-8"))
  51. data = f.read()
  52. print(data)
  53. print(type(data))
  54. # 运行结果
  55. '''
  56. b'\xe8\xbd\xa9\xe5\xa2\xa8\xe6\x98\xaf\xe6\x88\x91\xe8\xba\xab\xe4\xb8\x8b\xe5\x8f\x97'
  57. <class 'bytes'>
  58. '''

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. Python locale 多语言模块和我遇到的坑

    Table of Contents 1. locale遇到的问题 1.1. locale 简介 1.1.1. 什么是locale 1.1.2. locale 相关命令 1.2. Python loca ...

  2. java练习——多态与异常处理

    1.   上面的程序运行结果是什么? 2.   你如何解释会得到这样的输出? parent = chlid; 所以child中的方法被赋予parent,所以用child方法输出了child的成员变量: ...

  3. 2 semantic ui 框架的应用

    为什么使用css框架 1.使用基础样式 :  ui segment 分段:内容片段 <link rel="stylesheet" href="css/semanti ...

  4. 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 ...

  5. MySQL数据库基础总结

    来源: 实验楼 链接: https://www.shiyanlou.com/courses/9 一.开发准备 # 打开 MySQL 服务 sudo service mysql start #使用 ro ...

  6. dealloc时取weakself引起崩溃

    今天无意这中遇到一个奇怪的崩溃,先上引起崩溃的代码: - (void)dealloc { __weak __typeof(self)weak_self = self; NSLog(@"%@& ...

  7. b树的实现

    花了蛮长时间实现的b树插入操作.有时间再实现其他操作. #include <stdio.h> #include <stdlib.h> #define M 5 enum KeyS ...

  8. linux下多线程断点下载工具-axel

    今天要下载一下14G左右的文件,用wget约10小时,后来发现linux下有个多线程支持断点续传的下载工具axel,试了一下,下载速度大大增加. 包地址:http://pkgs.repoforge.o ...

  9. Python 模块:random 随机数生成

    Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...

  10. unity生命周期

    1.静态构造函数 当程序集被加载的时候就被调用了,如果你的unity处于编辑状态时,此时你保存一个脚本(从而迫使重新编译),静态构造函数会立即被调用,因为unity加载了DLL.并且它将不会再次运行, ...