本文整理匯總了Python中typing.TextIO.readline方法的典型用法代碼示例。如果您正苦於以下問題:Python TextIO.readline方法的具體用法?Python TextIO.readline怎麽用?Python TextIO.readline使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typing.TextIO
的用法示例。
在下文中一共展示了TextIO.readline方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: pat_dependency
# 需要導入模塊: from typing import TextIO [as 別名]
# 或者: from typing.TextIO import readline [as 別名]
def pat_dependency(src_path: str, src_file: TextIO) -> str:
'''
Return a list of dependencies.
A .pat file always has a single dependency: the source file it patches.
'''
version_line = src_file.readline()
orig_line = src_file.readline()
orig_path = orig_line.strip()
if not orig_path:
failF('pat error: {}:2:1: line specifying original path is missing or empty.', src_path)
return orig_path
示例2: output_file
# 需要導入模塊: from typing import TextIO [as 別名]
# 或者: from typing.TextIO import readline [as 別名]
def output_file(out: TextIO, fin: TextIO, keep_license: bool) -> None:
skip = LICENSE_LINES
if keep_license: skip = 0
while True:
line = fin.readline()
if not line:
break
if skip:
skip -= 1
continue
out.write(line)
示例3: stuff
# 需要導入模塊: from typing import TextIO [as 別名]
# 或者: from typing.TextIO import readline [as 別名]
def stuff(a: TextIO) -> str:
return a.readline()
示例4: read_game
# 需要導入模塊: from typing import TextIO [as 別名]
# 或者: from typing.TextIO import readline [as 別名]
def read_game(handle: TextIO, *, Visitor: Callable[[], BaseVisitor[ResultT]] = GameBuilder) -> Optional[ResultT]:
"""
Reads a game from a file opened in text mode.
>>> import chess.pgn
>>>
>>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn")
>>>
>>> first_game = chess.pgn.read_game(pgn)
>>> second_game = chess.pgn.read_game(pgn)
>>>
>>> first_game.headers["Event"]
'IBM Man-Machine, New York USA'
>>>
>>> # Iterate through all moves and play them on a board.
>>> board = first_game.board()
>>> for move in first_game.mainline_moves():
... board.push(move)
...
>>> board
Board('4r3/6P1/2p2P1k/1p6/pP2p1R1/P1B5/2P2K2/3r4 b - - 0 45')
By using text mode, the parser does not need to handle encodings. It is the
caller's responsibility to open the file with the correct encoding.
PGN files are usually ASCII or UTF-8 encoded. So, the following should
cover most relevant cases (ASCII, UTF-8, UTF-8 with BOM).
>>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn", encoding="utf-8-sig")
Use :class:`~io.StringIO` to parse games from a string.
>>> import io
>>>
>>> pgn = io.StringIO("1. e4 e5 2. Nf3 *")
>>> game = chess.pgn.read_game(pgn)
The end of a game is determined by a completely blank line or the end of
the file. (Of course, blank lines in comments are possible).
According to the PGN standard, at least the usual 7 header tags are
required for a valid game. This parser also handles games without any
headers just fine.
The parser is relatively forgiving when it comes to errors. It skips over
tokens it can not parse. Any exceptions are logged and collected in
:data:`Game.errors <chess.pgn.Game.errors>`. This behavior can be
:func:`overriden <chess.pgn.GameBuilder.handle_error>`.
Returns the parsed game or ``None`` if the end of file is reached.
"""
visitor = Visitor()
found_game = False
skipping_game = False
headers = None
managed_headers = None
# Ignore leading empty lines and comments.
line = handle.readline().lstrip("\ufeff")
while line.isspace() or line.startswith("%") or line.startswith(";"):
line = handle.readline()
# Parse game headers.
while line:
# Ignore comments.
if line.startswith("%") or line.startswith(";"):
line = handle.readline()
continue
# First token of the game.
if not found_game:
found_game = True
skipping_game = visitor.begin_game() is SKIP
if not skipping_game:
managed_headers = visitor.begin_headers()
if not isinstance(managed_headers, Headers):
managed_headers = None
headers = Headers({})
if not line.startswith("["):
break
if not skipping_game:
tag_match = TAG_REGEX.match(line)
if tag_match:
visitor.visit_header(tag_match.group(1), tag_match.group(2))
if headers is not None:
headers[tag_match.group(1)] = tag_match.group(2)
else:
break
line = handle.readline()
if not found_game:
return None
if not skipping_game:
skipping_game = visitor.end_headers() is SKIP
# Ignore single empty line after headers.
#.........這裏部分代碼省略.........