CKTIS / Python
io.TextIOBase
TextIOBase definuje rozhranie textových prúdov pracujúcich s typom str.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
io.TextIOBase()TextIOBase definuje rozhranie textových prúdov pracujúcich s typom str.
encoding označuje kódovanie, errors spôsob spracovania chýb a newlines konce riadkov nájdené pri univerzálnom čítaní. Pri TextIOWrapper sprístupňuje buffer binárny prúd. detach() ho vráti a obal už nemožno používať. StringIO nemá binárnu pamäť a detach() vyvolá io.UnsupportedOperation. TextIOWrapper dekóduje bajty podľa zvoleného kódovania. V UTF-8 vrátia tri volania read(1) postupne 'é', 'x' a na konci '', hoci 'é' zaberá dva bajty.
tell() môže vrátiť nepriehľadnú hodnotu na obnovenie pozície pomocou seek(), ktorá nie je indexom znaku.
import io
with io.TextIOWrapper(io.BytesIO('éx'.encode()),encoding='utf-8') as s:
assert isinstance(s,io.TextIOBase) and s.read(1)=='é'
cookie=s.tell()
assert s.read(2)=='x' and s.read(1)==''
assert s.seek(cookie)==cookie and s.read(1)=='x'
try: s.write(b'x')
except TypeError: pass
else: raise AssertionError
raw=io.BytesIO(b'a\r\n')
s=io.TextIOWrapper(raw,encoding='utf-8',errors='strict',newline=None)
assert (s.encoding,s.errors,s.read(),s.newlines)==('utf-8','strict','a\n','\r\n')
assert s.buffer is s.detach() is raw
try: s.read()
except ValueError: pass
else: raise AssertionError
raw.close()
with io.StringIO() as s:
try: s.detach()
except io.UnsupportedOperation: pass
else: raise AssertionError
Príklady
import io
with io.TextIOWrapper(io.BytesIO("éx".encode()), encoding="utf-8") as stream:
print(isinstance(stream, io.TextIOBase))
print(stream.read(1), stream.read(1), repr(stream.read(1)))
Výsledok
True é x ''
tell() môže vrátiť nepriehľadnú hodnotu na obnovenie pozície pomocou seek(), ktorá nie je indexom znaku.