1,105,581 Community Members

Reading rows of a CSV file

Member Avatar
novice20
Junior Poster
161 posts since Sep 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi,

I just came accross the csv module to deals with csv files in python.

suppose my csv file has two rows:

1,2,3,4
5,6,7,8

and i want to access each item at a time, i.e., 1 followed by 2 and so on....what can be done?

import csv
reader= csv.reader(open('filename.csv','r'),delimiter=' ')
for row in reader:
print row

gives me
[1,2,3,4]
[5,6,7,8]

I want to access a single value at a time

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Why not

for row in reader:
    for item in row:
        print item

?
Also learn about code tags here http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3.

Member Avatar
novice20
Junior Poster
161 posts since Sep 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Grib......... thanks for the reply, but it still gives me:

1,2,3,4
5,6,7,8

i want to access 1 first followed by 2, followed by 3 and so on....

Member Avatar
novice20
Junior Poster
161 posts since Sep 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I know there must be something pretty simple, but it's blocking me, need help :)

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

I know there must be something pretty simple, but it's blocking me, need help :)

You must use delimiter=',' .

Member Avatar
novice20
Junior Poster
161 posts since Sep 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok...it was that simple! I couldn't get it...now can i store the values in an array kind of structure? list is not helping me

Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 172 [?]
Skill Endorsements: 3 [?]
 
0
 

As list in python
So is array in c,c++

They are all the same stuff with different names. :)

Member Avatar
novice20
Junior Poster
161 posts since Sep 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok..thanks richieking:)

Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 172 [?]
Skill Endorsements: 3 [?]
 
0
 

You can close the thread as solve ...... :)
happy coding ***

Question Answered as of 3 Years Ago by richieking and Gribouillis
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article