View Single Post
  #1 (permalink)  
Old 04-23-2007, 12:11 PM
annannienann annannienann is offline
Newbie
 
Join Date: Apr 2007
Posts: 11
Rep Power: 0
annannienann is on a distinguished road
Question Python 2D array question

Hello, some of you may have read my first post...if you did I tired Python and love. It just so fast to program in compared to Java or VB (my 'mother tongue').

I am have bit of trouble with syntax...I think. I have almost managed to import a .cvs (in other words a text file) into the application and got part way to splitting it into a 2D array. Here is the code I have so far

print "Welcome To UNIT 6"
print "-----------------"

# Creates file object
file = open("C:\\Documents and Settings\Peter Sutton\Desktop\Practice Open Book Graphs.txt")

# Declare arrays
cell = []
array = []

# Reads each line from the file or each row from the spread sheet
for line in file:
temp = line.replace("\n","") # Removes the new line marker
while temp != "":
cell = temp.partition(",") # Splits the row read from the file into cells
if cell [1] != "":
array.append(cell[0]) # Adds the cells to the array if cells to add
temp = cell [2]
else:
temp = "" # Breaks while loop
print array # Prints the array for testing


The problem with this code is that it does not create an array that reflects the spread sheet. It stick the data in each cell into the array after the last.

I need to know how to make a proper 2D array and how to decide where the data from the .cvs file goes in the array.

I have supplied my test data so you can try my code. (Its my practice open book stuff for chemistry AS level )
Attached Files
File Type: txt Practice Open Book Graphs.txt (741 Bytes, 20 views)

Last edited by annannienann; 04-23-2007 at 12:18 PM.
Reply With Quote

Sponsored Links