zoukankan      html  css  js  c++  java
  • Python中StringIO和BytesIO

    介绍一下Python在内存中读写数据,用到的模块是StringIO和BytesIO

    StringIO

    >>> from io import StringIO
    >>> f = StringIO()
    >>> f.write('hello')
    5
    >>> f.write(' ')
    1
    >>> f.write('world!')
    6
    >>> print(f.getvalue())
    hello world!

    getvalue()方法用于获得写入后的str。

    要读取StringIO,可以用一个str初始化StringIO,然后,像读文件一样读取:

    >>> from io import StringIO
    >>> f = StringIO('Hello!
    Hi!
    Goodbye!')
    >>> while True:
    ...     s = f.readline()
    ...     if s == '':
    ...         break
    ...     print(s.strip())
    ...
    Hello!
    Hi!
    Goodbye!

    BytesIO

    StringIO操作的只能是str,如果要操作二进制数据,就需要使用BytesIO。

    BytesIO实现了在内存中读写bytes,我们创建一个BytesIO,然后写入一些bytes:

    >>> from io import BytesIO
    >>> f = BytesIO()
    >>> f.write('中文'.encode('utf-8'))
    6
    >>> print(f.getvalue())
    b'xe4xb8xadxe6x96x87'

    请注意,写入的不是str,而是经过UTF-8编码的bytes。

    和StringIO类似,可以用一个bytes初始化BytesIO,然后,像读文件一样读取:

    >>> from io import BytesIO
    >>> f = BytesIO(b'xe4xb8xadxe6x96x87')
    >>> f.read()
    b'xe4xb8xadxe6x96x87'

    总结

    # stringIO 比如说,这时候,你需要对获取到的数据进行操作,但是你并不想把数据写到本地硬盘上,这时候你就可以用stringIO
    >>> from io import StringIO
    >>> from io import BytesIO
    >>> def outputstring():
        return 'string 
    from 
    outputstring 
    function'
    
    >>> s = outputstring()
    # 将函数返回的数据在内存中读
    >>> sio = StringIO(s)
    # 可以用StringIO本身的方法
    >>> print(sio.getvalue())
    string 
    from 
    outputstring 
    function
    # 也可以用file-like object的方法
    >>> s= sio.readlines()
    >>> for i in s:
        print(i.strip())
    
    >>>string
    from
    outputstring
    function
    # 将函数返回的数据在内存中写
    >>> sio = StringIO()
    >>> sio.write(s)
    36
    # 可以用StringIO本身的方法查看
    >>> s = sio.getvalue()
    >>> print(s)
    string 
    from 
    outputstring 
    function
    # 如果你用file-like object的方法查看的时候,你会发现数据为空
    >>> sio = StringIO()
    >>> sio.write(s)
    36
    >>> for i in sio.readlines():
        print(i.strip())
    
        
    >>> 
    # 这时候我们需要修改下文件的指针位置
    # 我们发现可以打印出内容了
    >>> sio = StringIO()
    >>> sio.write(s)
    36
    >>> sio.seek(0,0)
    0
    >>> print(sio.tell())
    0
    >>> for i in sio.readlines():
        print(i.strip())
    
        
    string
    from
    outputstring
    function
    # 这就涉及到了两个方法seek 和 tell
    # tell 方法获取当前文件读取指针的位置
    # seek 方法,用于移动文件读写指针到指定位置,有两个参数,第一个offset: 偏移量,需要向前或向后的字节数,正为向后,负为向前;第二个whence: 可选值,默认为0,表示文件开头,1表示相对于当前的位置,2表示文件末尾
    # 用seek方法时,需注意,如果你打开的文件没有用'b'的方式打开,则offset无法使用负值哦
    # stringIO 只能操作str,如果要操作二进制数据,就需要用到BytesIO # 上面的sio无法用seek从当前位置向前移动,这时候,我们用'b'的方式写入数据,就可以向前移动了
    >>> bio = BytesIO()
    >>> bio.write(s.encode('utf-8'))
    36
    >>> print(bio.getvalue())
    b'string 
    from 
    outputstring 
    function'
    >>> bio.seek(-36,1)
    0
    >>> print(bio.tell())
    0
    >>> for i in bio.readlines():
          print(i.strip())
    
        
    b'string'
    b'from'
    b'outputstring'
    b'function'
    
    

     引自廖雪峰Python教程

     
    人生苦短,何不用python
  • 相关阅读:
    (原创) mac 10.9.2 eclipse 的 CDT 的 异常的修复
    (转) Virtual function
    (转) ROS NAMING AND NAMESPACES
    (转) Data structures
    (转) Dynamic memory
    java string类
    eclipse 的快捷键
    java抽象类和接口
    面向对象的三大特征
    Java 中的多态
  • 原文地址:https://www.cnblogs.com/yqpy/p/8556090.html
Copyright © 2011-2022 走看看