5

我无法通过 Python 文档弄清楚,所以也许我可以在这里问它:

import StringIO
line = StringIO.StringIO()
line.write('Hello World')

有没有我可以使用的方法,可以line[1:]对字符串执行什么操作,所以line.getvalue()会返回ello World

谢谢。

4

2 回答 2

5

我不知道如何使用line.getvalue,但您可以使用 StringIO 对象,如普通文件对象。只需寻找字节 1 并像往常一样阅读。

>>> import StringIO
>>> line = StringIO.StringIO()
>>> line.write("Hello World")
>>> line.seek(0)
>>> print line.getvalue()
Hello World
>>> line.seek(1)
>>> print line.getvalue()
Hello World
>>> line.seek(1)
>>> print next(line)
ello World
>>> line.seek(1)
>>> print line.read()
ello World
于 2013-06-21T13:19:55.613 回答
2

StringIO getvalue() 函数将内容作为字符串返回,所以这可以工作:

content = line.getvalue()
print content[1:]
于 2013-06-21T13:33:02.963 回答