We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,931 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Python school project program HELP!!!

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

3
Contributors
3
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
caseyt88
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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]]
'''
vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

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

woooee
Posting Maven
2,705 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9

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!

caseyt88
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by vegaseat and woooee

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0675 seconds using 2.7MB