|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 print array # Prints the array for testingcell = temp.partition(",") # Splits the row read from the file into cells 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 )Last edited by annannienann; 04-23-2007 at 12:18 PM. |
| Sponsored Links |
|
|
|
|||
|
Hi annannienann,
For CSV's I would take a good look at the csv module documentation, but, looking at your txt file input you should be able to use the .split() string method to split the input on commas, and, because this returns a list of strings, you could use a list of lists as a 2D array something like the following (untested): Code:
txtfile = ...
data = []
for line in txtfile:
data.append( line.rstrip().split(',') )
P.S. It is not good to re-assign file - it is a Python built-in function. (Thats why I have used txtfile). |
|
|||
|
Thanks everyone. Cracked it..
import csv reader = csv.reader(open("C:\\Documents and Settings\Peter Sutton\Desktop\Practice Open Book Graphs.txt", "rb")) array = [] for row in reader: array.append( row ) print array |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to learn Python. | Sir_Rimo | Python | 21 | 08-22-2008 09:16 AM |
| fread into array position | kenna | C and C++ | 0 | 08-17-2007 09:03 AM |
| Python arrays... | Sir_Rimo | Python | 3 | 06-20-2007 09:54 AM |
| Python Collection | reachpradeep | Python | 1 | 03-03-2007 03:50 PM |
| Is string in array? | smith | Perl | 2 | 02-14-2007 02:30 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |