简体   繁体   English

在Python中将文本文件读取到二维列表中,而不删除空格

[英]Reading a text file into a 2d list in Python, without deleting spaces

So I'm working on a project for school and we will be given a text file similar to the one shown here: 所以我正在为一个学校项目工作,我们将得到一个类似于此处所示的文本文件:

+---+-----+-----+
|   |     |     |
|   |     |     |
|   |     +=====+
+---+     |  |  |
|   |     |  |  |
|   +-+   +=====+
|   | |   |     |
|   | +-+ |     |
|   |   | |     |
|   |   +-+     |
|   |     |_____|
|   |     |     |
+---+-----+-----+

What I am having trouble with is reading the text to a 2D list. 我遇到的麻烦是将文本读取到2D列表中。 Whenever I try it either creates a list of one very long string like this: 每当我尝试它时,都会创建一个很长的字符串列表,如下所示:

This code: 这段代码:

    with open(fileName) as f:
        grid = f.read().split("/n")

Gives this output: 给出以下输出:

['+---+-----+-----+\n|   |     |     |\n|   |     |     |\n|   |     +=====+\n+---+     |  |  |\n|   |     |  |  |\n|   +-+   +=====+\n|   | |   |     |\n|   | +-+ |     |\n|   |   | |     |\n|   |   +-+     |\n|   |     |_____|\n|   |     |     |\n+---+-----+-----+']

or something where it deletes spaces: 或删除空格的内容:

Where this code: 此处的代码:

with open(fileName) as f:
    grid = f.read().split()

Gives this: 给出以下内容:

['+---+-----+-----+', '|', '|', '|', '|', '|', '|', '|', '|', '|', '|', '+=====+', '+---+', '|', '|', '|', '|', '|', '|', '|', '|', '|', '+-+', '+=====+', '|', '|', '|', '|', '|', '|', '|', '+-+', '|', '|', '|', '|', '|', '|', '|', '|', '|', '+-+', '|', '|', '|', '|_____|', '|', '|', '|', '|', '+---+-----+-----+']

What I need is a 2D list that includes EVERY character, (yes, even spaces) so that I may manipulate each one at a later point. 我需要的是一个2D列表,其中包含每个字符(是,甚至是空格),以便以后可以对每个字符进行操作。 This is what I'm looking for: 这就是我要寻找的:

[['+','-','-','-','+','-','-','-','-','-','+','-','-','-','-','-','+']
['|',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' ',' ',' ','+']
['|',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' ',' ',' ','+']
.... etc

Sorry if this is easy, I'm fairly new to programming. 抱歉,如果这很容易,我是编程新手。 Also I have looked through other questions on here and couldn't find one similar to what I needed, so sorry in advance if this has already been answered. 另外,我在这里浏览了其他问题,但找不到与我需要的问题类似的问题,如果对此问题已经得到解答,请提前抱歉。

Like this? 像这样?

>>> with open('file') as f:
...     lines = f.read().splitlines()

>>> [list(i) for i in lines]
[['+', '-', '-', '-', '+', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '+'],
 ['|', ' ', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | '],
 [' | ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | '],
 [' | ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', '+', ' = ', ' = ', ' = ', ' = ', ' = ', '+'],
 ['+', '-', '-', '-', '+', ' ', ' ', ' ', ' ', ' ', ' | ', ' ', ' ', ' |', ' ', ' ', '|'],
 ['|', ' ', ' ', ' ', '|', ' ', ' ', ' ', ' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|'],
 ['|', ' ', ' ', ' ', '+', '-', '+', ' ', ' ', ' ', '+', ' = ', ' = ', ' = ', ' = ', ' = ', '+'],
 [' | ', ' ', ' ', ' ', ' | ', ' ', ' | ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | '],
 [' | ', ' ', ' ', ' ', ' | ', ' ', '+', '-', '+', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | '],
 [' | ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' | ', ' ', ' |', ' ', ' ', ' ', ' ', ' ', '|'],
 ['|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '+', '-', '+', ' ', ' ', ' ', ' ', ' ', '|'],
 ['|', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | ', '_', '_', '_', '_', '_', ' | '],
 [' | ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', ' | ', ' ', ' ', ' ', ' ', ' ', '|'],
 ['+', '-', '-', '-', '+', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '+']]

Given that this is a school project, I am intentionally avoiding posting the full solution, so here are some hints that should be sufficient to figure it out: 鉴于这是一个学校项目,我有意避免发布完整的解决方案,因此这里有一些提示应该足以弄清楚:

  • "/n" is not a newline. "/n"不是换行符。 Study the output string to see what newline is . 研究输出字符串以了解换行符什么。 This way, you are splitting on something that is not found in the string, which gives you a one-element array of everything. 这样,您就可以拆分字符串中未找到的内容,从而为您提供所有内容的单元素数组。

  • .split() splits on all whitespace. .split()在所有空格上分割。 Not what you want. 不是你想要的。 The first attempt is better, but see above. 第一次尝试会更好,但请参见上文。

  • When you split the file into lines, you need to split each line into characters. 将文件拆分为几行时,需要将每一行拆分为字符。 list(line) will do that for a single line. list(line)将针对单个行执行此操作。 Study either list comprehensions or the map function to figure out how to apply list on every line in the split result. 研究列表推导或map功能,以弄清楚如何在拆分结果的每一行上应用list

read line by line, as: 逐行读取,如下所示:

for line in f:
    print(line)

You might not need to create a list from a string as you can use index [] on string directly. 您可能不需要从字符串创建列表,因为可以直接在字符串上使用index []。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM