No documentation on reading in int from file

Thread Solved

Join Date: Apr 2008
Posts: 8
Reputation: bachmabt is an unknown quantity at this point 
Solved Threads: 1
bachmabt bachmabt is offline Offline
Newbie Poster

No documentation on reading in int from file

 
0
  #1
Apr 5th, 2008
This seems like a very simple process, but I cannot get the data out of the loop:


I have a data file "slink.txt":

  1. 88
  2. 10112213
  3. 33332332
  4. 22011220
  5. 23110122
  6. 21231102
  7. 12222321
  8. 32131132
  9. 10023232

I am trying to store the numbers in a 2 dimensional array:

  1. f = open('slink.txt', 'r')
  2.  
  3. rows = int(f.read(1))
  4. cols = int(f.read(1))
  5. f.read(1) #skip endline
  6. temp = []
  7. data = []
  8.  
  9. for row in range(rows):
  10. for col in range(cols):
  11. temp.append(int(f.read(1)))
  12. f.read(1)
  13. data.append(temp)
  14. print '\n\nRow:', row, temp, '\nData: ', data #debug
  15. del temp[:]
  16.  
  17.  
  18. print '\n\n\n',data

A sample of my output shows:


  1. Row: 0 [1, 0, 1, 1, 2, 2, 1, 3]
  2. Data: [[1, 0, 1, 1, 2, 2, 1, 3]]
  3.  
  4.  
  5. Row: 1 [3, 3, 3, 3, 2, 3, 3, 2]
  6. Data: [[3, 3, 3, 3, 2, 3, 3, 2], [3, 3, 3, 3, 2, 3, 3, 2]]
  7.  
  8.  
  9. Row: 2 [2, 2, 0, 1, 1, 2, 2, 0]
  10. Data: [[2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0]]
  11.  
  12. .
  13. .
  14. .
  15.  
  16. [[], [], [], [], [], [], [], []]

The last line shows the data variable to be empty. Something doesn't seem right. Please help.

Ben
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: No documentation on reading in int from file

 
0
  #2
Apr 5th, 2008
It's got to be the way your data file has been written. Try this ...
  1. str1 = """\
  2. 88
  3. 10112213
  4. 33332332
  5. 22011220
  6. 23110122
  7. 21231102
  8. 12222321
  9. 32131132
  10. 10023232
  11. """
  12. # save your data file
  13. fout = open('slink.txt', 'w')
  14. fout.write(str1)
  15. fout.close()
  16.  
  17.  
  18. f = open('slink.txt', 'r')
  19.  
  20. rows = int(f.read(1))
  21. cols = int(f.read(1))
  22. f.read(1) #skip endline
  23. temp = []
  24. data = []
  25.  
  26. for row in range(rows):
  27. for col in range(cols):
  28. temp.append(int(f.read(1)))
  29. f.read(1)
  30. data.append(temp)
  31. print '\nRow:', row, temp, '\nData: ', data #debug
  32. temp = [] # or del temp[:]
  33.  
  34. print '\n\n\n',data
Last edited by vegaseat; Apr 5th, 2008 at 7:30 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: bachmabt is an unknown quantity at this point 
Solved Threads: 1
bachmabt bachmabt is offline Offline
Newbie Poster

Re: No documentation on reading in int from file

 
0
  #3
Apr 5th, 2008
Not sure it has anything to do with the data file. I tried the same algorithm using a 2d dummy variable, instead of reading data in from a file, with only the same results. Maybe something with the append module?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: bachmabt is an unknown quantity at this point 
Solved Threads: 1
bachmabt bachmabt is offline Offline
Newbie Poster

Re: No documentation on reading in int from file

 
0
  #4
Apr 5th, 2008
Its the del temp [:] that is messing with the append function.

When I substitute your temp = [] everything works well...


Thank you
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 4
Reputation: Mike W is an unknown quantity at this point 
Solved Threads: 1
Mike W Mike W is offline Offline
Newbie Poster

Re: No documentation on reading in int from file

 
0
  #5
Apr 6th, 2008
I believe this is due to the way Python handles variables.

When you append an element, you are effectively appending a reference, in this case a reference to an array. Each time you run the loop, you empty the array.

When you write del temp [:] you are telling Python to delete the contents of the variable pointed to by temp.

When you write temp = [], you are telling Python to make temp now point to a new empty array.

Hmm, that wasn't a very good explanation. Here are a couple of examples. First of all:

  1. >>> a = [1]
  2. >>> b = []
  3. >>> b.append(a)
  4. >>> b
  5. [[1]]
  6. >>> a.append(2)
  7. >>> b
  8. [[1, 2]]

Here we say a points to an array consisting of just 1, and append it to b, which points to an empty list. When we inspect the contents of b, it's what we expect.

Then, if we append 2 to a, we actually append 2 to the array pointed to by a - this is the same array that is pointed to in the first element of b, so the first element of b changes as well.

If instead we use this code:

  1. >>> a = [1]
  2. >>> b = []
  3. >>> b.append(a)
  4. >>> b
  5. [[1]]
  6. >>> a = [1,2]
  7. >>> b
  8. [[1]]

It behaves differently. Rather than changing the array that is pointed to by a, a = [1,2] says that we create a brand new array, containing 1 and 2, and make a point to this new array. Therefore, the array that a used to point to is unchanged, and the first element of the b is unchanged.

Similarly, when you write del temp[:] you affect an element of data, since this changes the object that both temp and an element of data point to. When you write temp = [], you are reassigning temp so that it points to something else, which therefore does not affect any elements of data.

Hopefully that makes some sort of sense!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 8
Reputation: bachmabt is an unknown quantity at this point 
Solved Threads: 1
bachmabt bachmabt is offline Offline
Newbie Poster

Re: No documentation on reading in int from file

 
0
  #6
Apr 6th, 2008
Thank you, I appreciate the explination. I kind of figured it was a pointer issue when I learned not to use the del command. I'll tell you, it was confusing the heck out of me before I posted...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC