Exemplo n.º 1
0
    print()
# SeekableUnicodeStreamReader
stream = BytesIO(b"""
This is a test file.
It is encoded in ascii.
""".decode('ascii').encode('ascii'))
reader = SeekableUnicodeStreamReader(stream, 'ascii')
print(reader.read())  # read the entire file.
print(reader.seek(0))  # rewind to the start.
print(reader.read(5))  # read at most 5 bytes.
print(reader.readline())  # read to the end of the line.
print(reader.seek(0))  # rewind to the start.
for line in reader:
    print(repr(line))  # iterate over lines
print(reader.seek(0))  # rewind to the start.
print(reader.readlines())  # read a list of line strings
print(reader.close())
# size argument to read()
stream = BytesIO(b"""
This is a test file.
It is encoded in utf-16.
""".decode('ascii').encode('utf-16'))
reader = SeekableUnicodeStreamReader(stream, 'utf-16')
print(reader.read(10))
print(reader.seek(0))  # rewind to the start.
print(reader.read(1))  # we actually need to read 4 bytes
print(int(reader.tell()))
print(reader.seek(0))  # rewind to the start.
print(reader.readline())  # stores extra text in a buffer
print(reader.linebuffer)  # examine the buffer contents
print(reader.read(0))  # returns the contents of the buffer