从给定的 txt 文件获取 X 和 Y 尺寸

Getting X & Y dimensions from given txt file

我在 Python 中上传了一个 txt 文件(一个迷宫)到我的代码中。

示例:

10 8

+-+-+-+-+-+-+-+-+-+-+
|*      |           |
+ +-+-+ +     +-+ + +
|   |         |X  | |
+-+ + +-+     +-+-+ +
|       |     |     |
+-+-+-+-+ + + +-+   +
|         | |   |   |
+ +-+-+ +-+ +-+ +   +
| |       | |   |   |
+ + +-+-+ + + +-+   +
| |   | | | |   |   |
+ +-+ + +-+ +-+ +   +
| |       | |   |   |
+ +-+-+-+-+ + +-+   +
|                   |
+-+-+-+-+-+-+-+-+-+-+

我愿意保存第一行——迷宫的尺寸。 我编写的代码只有在每个维度上都有一个数字时才有效。无论每个维度中有多少数字,我如何获得维度。 在上面的示例中,我想得到 10 和 8。

我的代码:

def loadMaze(file_name):
    readIt = open(file_name, 'r')
    readLines = readIt.readlines()
    x_dim = int(readLines[0][0])
    y_dim = int(readLines[0][2])
    mazeList = [list(i.strip()) for i in readLines[1:]]
    return x_dim, y_dim, mazeList

假设迷宫维度始终是文本文件的第一行,您可以使用字符串 split() 方法在任何空白处拆分第一行,返回一个列表。列表的第一个元素是 x 维度;第二个元素是 y 维度。

>>> def load_maze(filename):
    with open(filename) as file:
        lines = file.readlines()
    return lines

>>> maze = load_maze(path)
>>> x, y = maze[0].strip().split()
>>> x, y = int(x), int(y)
>>> x, y
(10, 8)

我对 python 的经验不多,但看起来你可以使用 .partition() 函数来获取字符串字符直到某个符号。然后你可以使用 .split()map() 函数将数字用空格分隔,然后将它们转换为 int

def loadMaze(file_name):
    readIt = open(file_name, 'r')
    readLines = readIt.readlines()
    partition = readLines[0].partition('+').split()
    dimensionArray = map(int, partition)
    x_dim = dimensionArray[0]
    y_dim = dimensionArray[1]
    mazeList = [list(i.strip()) for i in readLines[1:]]
    return x_dim, y_dim, mazeList

这里有一些参考资料

partition

split and map

编辑:之前查看您的文件时,我认为尺寸与迷宫的起点在同一线上。如果不是,则不需要分区

def loadMaze(file_name):
    readIt = open(file_name, 'r')
    readLines = readIt.readlines()
    splitFirstLine = readLines[0].split()
    dimensionArray = map(int, splitFirstLine)
    x_dim = dimensionArray[0]
    y_dim = dimensionArray[1]
    mazeList = [list(i.strip()) for i in readLines[1:]]
    return x_dim, y_dim, mazeList

您永远不会关闭打开的文件。如docs中所述:

It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point.

您的其余代码可以使用 str.stplit and multiple assignment 简洁地完成。 (下划线被分配到维度和迷宫之间的空白行,作为“忽略”它的一种方式,因为你可能不希望它出现在迷宫列表中。)

def load_maze(file_name):
    with open(file_name) as f:
        dims, _, *maze_lines = [line.rstrip() for line in f]
    x, y = [int(dim) for dim in dims.split()]
    maze = [list(line) for line in maze_lines]
    return x, y, maze

我个人认为 return xy 作为一个元组在一起可能会更好,像这样:

return (x, y), maze

但这取决于你。

您的代码存在的问题是,当您执行 readLines[0][0] 时,您实际指向的是字符串中的一个字符。请注意 ^ 表示您正在阅读的是哪个字符。

10 8\n
^

对于 readLines[0][2] 你实际指向的是字符串中的一个字符:

10 8\n
  ^

您要做的是解析该行,在 space 上分开并将这些标记(10 和 8)视为整数。 我还建议使用“with open”,因为您永远不会“关闭”您的文件。 这将采用第一个字符串,拆分为 spaces 以形成标记列表。然后您可以访问数字(作为字符串)并转换为整数。

def loadMaze(file_name):
    with open(file_name, 'r') as  readIt:
        readLines = readIt.readlines()
    dims = readLines[0].split()
    x_dim = int(dims[0])
    y_dim = int(dims[1])
    mazeList = [list(i.strip()) for i in readLines[1:]]
    return x_dim, y_dim, mazeList

或者您可以在阅读文件时执行此操作

def loadMaze(file_name):
    with open(file_name, 'r') as  readIt:
       dims = readIt.readline().split()
       x_dim = int(dims[0])
       y_dim = int(dims[1])
       readIt.readline() # read and skip the blank line
       mazeList = [list(i.strip()) for i in readIt.readlines()]
    return x_dim, y_dim, mazeList