Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Python

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-2007, 11:11 AM
annannienann annannienann is offline
Newbie
 
Join Date: Apr 2007
Posts: 11
Credits: 0
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, 7 views)

Last edited by annannienann; 04-23-2007 at 11:18 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-23-2007, 03:29 PM
paddy3118 paddy3118 is offline
Newbie
 
Join Date: Apr 2007
Location: UK
Posts: 6
Credits: 0
Rep Power: 0
paddy3118 is on a distinguished road
Wink

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(',') )
- Paddy.
P.S. It is not good to re-assign file - it is a Python built-in function. (Thats why I have used txtfile).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-23-2007, 03:36 PM
Ludwig's Avatar   
Ludwig Ludwig is offline
Newbie
 
Join Date: Apr 2007
Posts: 2
Credits: 0
Rep Power: 0
Ludwig is on a distinguished road
Wink

Python actually has a csv module. Read the Python manual on it, and it will make your life much easier!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-23-2007, 04:36 PM
annannienann annannienann is offline
Newbie
 
Join Date: Apr 2007
Posts: 11
Credits: 0
Rep Power: 0
annannienann is on a distinguished road
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to learn Python. Sir_Rimo Python 21 08-22-2008 08:16 AM
fread into array position kenna C and C++ 0 08-17-2007 08:03 AM
Python arrays... Sir_Rimo Python 3 06-20-2007 08:54 AM
Python Collection reachpradeep Python 1 03-03-2007 02:50 PM
Is string in array? smith Perl 2 02-14-2007 01:30 PM


All times are GMT -5. The time now is 04:44 AM.

Contest Stats

Xav ........ 1097.16
MeTh0Dz|Reb0rn ........ 986.37
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 684.54
marwex89 ........ 638.26
Brandon W ........ 493.36
chili5 ........ 292.12
Steve.L ........ 188.37
orjan ........ 187.41

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads