本文整理汇总了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.
#.........这里部分代码省略.........