1 Replies - 17988 Views - Last Post: 30 August 2008 - 11:37 PM Rate Topic: -----

#1 rancor  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 30-November 07

Reading from a text file into 2D array

Posted 30 August 2008 - 02:06 PM

I've been trying to read floating point literals into a 2D array and have unusual results. When i read this file:
1 1.5 0 2
3 3.25 3 1.67

the code below prints out the list [[1.0, 1.5, 0.0, 2.0], [3.0, 3.25, 3.0, 1.6699999999999999]] . If anyone has any suggestions as to why the final number is approximated or knows of a better way of reading the file, please let me know. Thanks!

def main():
	DEFAULT_DIR = <put the directory where you store the text file>
	arr = []
	f = open(DEFAULT_DIR + 'input.txt', 'r')
	for line in f:
		arr.append(map(float, line.split()))
		
	print arr

if __name__ == '__main__':
	main()



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file into 2D array

#2 paddy3118  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-May 07

Re: Reading from a text file into 2D array

Posted 30 August 2008 - 11:37 PM

View Postrancor, on 30 Aug, 2008 - 02:06 PM, said:

I've been trying to read floating point literals into a 2D array and have unusual results. When i read this file:
1 1.5 0 2
3 3.25 3 1.67

the code below prints out the list [[1.0, 1.5, 0.0, 2.0], [3.0, 3.25, 3.0, 1.6699999999999999]] . If anyone has any suggestions as to why the final number is approximated or knows of a better way of reading the file, please let me know. Thanks!


Try this:
http://www.python.or...tut/node16.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1