阅读 sys.stdin 中的最后一行
Read last line in sys.stdin
对于编码问题,我必须使用 sys.stdin 读取数据。但是,我使用的方法是循环遍历 sys.stdin 并且失败,因为在输入测试用例后输入不包含换行符。
示例输入:
10 20
#################---
##-###############--
#---################
##-#################
########---#########
#######-----########
########---#########
##################--
#################---
##################-#
3 10
#-########
----------
#-########
用于读取输入的代码:
grids = []
for line in stdin:
if line == "\n":
break
m, n = map(int, line.split())
grid = []
for row in range(m):
r = list(stdin.readline().strip())
print("ROW:",r)
grid.append(r)
grids.append(grid)
return grids
当前输出:
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '#']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
期望的输出:
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '#']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
可以看出,最后一行没有打印,因此也没有被读取,因为换行符不包含在输入中。因此,我有什么办法可以阅读最后一行吗?请指教
我正在使用 Pycharm 社区版 2017。
编辑:
当我在 IDLE shell 中 运行 时解决了这个问题,我怀疑使用的 Pycharm 版本有一些错误,导致了这个错误。不管怎样,谢谢你的帮助。
检查您的输入是否在末尾包含“\n”。
来自 python 文档的片段:
https://docs.python.org/3/tutorial/inputoutput.html?highlight=readline
f.readline() reads a single line from the file; a newline character
(\n) is left at the end of the string, and is only omitted on the last
line of the file if the file doesn’t end in a newline. This makes
the return value unambiguous
对于编码问题,我必须使用 sys.stdin 读取数据。但是,我使用的方法是循环遍历 sys.stdin 并且失败,因为在输入测试用例后输入不包含换行符。
示例输入:
10 20
#################---
##-###############--
#---################
##-#################
########---#########
#######-----########
########---#########
##################--
#################---
##################-#
3 10
#-########
----------
#-########
用于读取输入的代码:
grids = []
for line in stdin:
if line == "\n":
break
m, n = map(int, line.split())
grid = []
for row in range(m):
r = list(stdin.readline().strip())
print("ROW:",r)
grid.append(r)
grids.append(grid)
return grids
当前输出:
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '#']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
期望的输出:
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-', '#', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '-', '-']
ROW: ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '-', '#']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
ROW: ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
ROW: ['#', '-', '#', '#', '#', '#', '#', '#', '#', '#']
可以看出,最后一行没有打印,因此也没有被读取,因为换行符不包含在输入中。因此,我有什么办法可以阅读最后一行吗?请指教
我正在使用 Pycharm 社区版 2017。
编辑:
当我在 IDLE shell 中 运行 时解决了这个问题,我怀疑使用的 Pycharm 版本有一些错误,导致了这个错误。不管怎样,谢谢你的帮助。
检查您的输入是否在末尾包含“\n”。
来自 python 文档的片段: https://docs.python.org/3/tutorial/inputoutput.html?highlight=readline
f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous