CKTIS / Python
io.TextIOBase
TextIOBase definuje rozhraní textových proudů pracujících s typem str.
Verze: Python 3.14; repository image, production runtime not attested
Syntaxe
io.TextIOBase()TextIOBase definuje rozhraní textových proudů pracujících s typem str.
encoding označuje kódování, errors způsob zpracování chyb a newlines konce řádků nalezené při univerzálním čtení. U TextIOWrapper zpřístupňuje buffer binární proud. detach() jej vrátí a obal již nelze používat. StringIO nemá binární paměť a detach() vyvolá io.UnsupportedOperation. TextIOWrapper dekóduje bajty podle zvoleného kódování. V UTF-8 vrátí tři volání read(1) postupně 'é', 'x' a na konci '', přestože 'é' zabírá dva bajty.
tell() může vrátit neprůhlednou hodnotu pro obnovení pozice pomocí seek(), která není indexem 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
Pří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ýsledek
True é x ''
tell() může vrátit neprůhlednou hodnotu pro obnovení pozice pomocí seek(), která není indexem znaku.