Hello, I am taking a computer programming class this semester and working on a project. I am trying to read a text file into a 2D list. The text file contains ten rows and ten columns of numbers, either a 0,1,or 2. When i try to read the file it adds the numbers on to the end of the list...this is what i have

def getfromfile():
      infile = input("Enter the name of the file with the original state of the network: ")

      infile = open(infile,"r")

      OLD =[ [" "]*COLUMN  for i in range(ROW)]
      for line in infile:
            for i in range(ROW):
                  OLD.append(i)
                  for j in range(COLUMN):
                        OLD.append(j)
      for i in range(ROW):
            for j in range(COLUMN):
                  if i == 0:
                        OLD[i][j] = DEAD
                                    
                  elif i == 1:
                        OLD[i][j] = CLEAN
                  else:
                        OLD[i][j] = INFECTED

The whole idea is that the list is a network of computers that are infected. OLD is the list of computers that is the network and as each time cycle passes, more computers get infected...i am just having a problem getting the "original state" from the text file...

THANKS!!!

Here is one way to load a 10x10 list with integers from a text file ...

""" assume int10x10.txt looks like this -->
1212000200
1010222122
1100202011
2021112212
0221022122
2012101022
2020021121
0112202210
0122110121
1010102201
"""

import pprint

infile = open("int10x10.txt", "r")

# create a 10x10 list of zeros
list10x10 = [[0]*10 for k in range(10)]

for row, line in enumerate(infile):
    # strip off trailing newline char
    line = line.rstrip()
    #print(line)  # test
    for col, c in enumerate(line):
        #print(c, row, col)  # test
        # load the 10x10 list with integers
        list10x10[row][col] = int(c)

pprint.pprint(list10x10)

''' my result -->
[[1, 2, 1, 2, 0, 0, 0, 2, 0, 0],
 [1, 0, 1, 0, 2, 2, 2, 1, 2, 2],
 [1, 1, 0, 0, 2, 0, 2, 0, 1, 1],
 [2, 0, 2, 1, 1, 1, 2, 2, 1, 2],
 [0, 2, 2, 1, 0, 2, 2, 1, 2, 2],
 [2, 0, 1, 2, 1, 0, 1, 0, 2, 2],
 [2, 0, 2, 0, 0, 2, 1, 1, 2, 1],
 [0, 1, 1, 2, 2, 0, 2, 2, 1, 0],
 [0, 1, 2, 2, 1, 1, 0, 1, 2, 1],
 [1, 0, 1, 0, 1, 0, 2, 2, 0, 1]]
'''

I am trying to read a text file into a 2D list.

Assuming you want each record to be one row of the 2D list, you are making your code too complicated.

infile = open(infile,"r")
 
    old_2D =[]
    for line in infile:
        line = line.strip()
        new_row = []          ## start with an empty list for each record

        ## you can also use
        ## for num in line:
        ##     new_row.append(num)
        ## or new_row = list(line)
        for ctr in range(len(line)):
              new_row.append(line[ctr])

        ## this row is finished so append it to old_2D
        old_2D.append(new_row)

    print old_2D

You should read the Python style guide as it makes everyone's code easier to read (i.e. variable names are lower case, and don't use "i', "l", or "o" as variable names as they can look like number or other letters especially at first glance "i"-->"l"-->"1").

well luckily the shell froze...again! Before though I switched it around to what vegaseat had but it gave me a value error for int(c)...here is what the text file looks like:

2 0 0 0 2 0 0 0 2 0
0 1 0 0 1 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
2 1 0 0 1 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 0 1 0 0
2 0 0 0 2 0 0 0 2 0
0 0 0 0 0 0 0 0 0 0

python is really starting to get to me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.