In Python, how do I check the size of a StringIO object?

前端 未结 2 1115
生来不讨喜
生来不讨喜 2021-01-31 13:56

And get the bytes of that StringIO object?

2条回答
  •  不要未来只要你来
    2021-01-31 14:42

    By checking the len attribute and using the getvalue() method

    Type "help", "copyright", "credits" or "license" for more information.
    >>> import StringIO
    >>> s = StringIO.StringIO()
    >>> s.write("foobar")
    >>> s.len
    6
    >>> s.write(" and spameggs")
    >>> s.len
    19
    >>> s.getvalue()
    'foobar and spameggs'
    

提交回复
热议问题