Tyler Sturtz
Tyler Sturtz

Reputation: 23

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. 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. 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.

Upvotes: 2

Views: 737

Answers (3)

Remi Guan
Remi Guan

Reputation: 22312

Like this?

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

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

Upvotes: 1

Muposat
Muposat

Reputation: 1506

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.

Upvotes: 0

Amadan
Amadan

Reputation: 198486

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. 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. 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. Study either list comprehensions or the map function to figure out how to apply list on every line in the split result.

Upvotes: 0

Related Questions