我有一个包含以下内容的测试代码:
with open('master.log') as f:
print(f.read(8))
print(f.read(8))
这打印为:
>> pi@raspberrypi:~/workspace/Program $ sudo python test.py
>> 12/29/20
>> 17 12:52
如您所见,这具有不同的打印效果。但是,当我这样做时:
import cStringIO
stream= "1234567890"
print(cStringIO.StringIO(stream).read(8))
print(cStringIO.StringIO(stream).read(8))
当我运行它时,我得到以下输出:
>> pi@raspberrypi:~/workspace/Program $ sudo python test.py
>> 12345678
>> 12345678
在这种情况下,它输出相同的值(搜索器不前进)。
我需要使 cStringIO (或类似的解决方案)以与文件相同的方式读取字符串。我的意思是每次阅读都没有重置位置。