Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

CSV file operations.

python cvs

  • Please log in to reply
4 replies to this topic

#1 Alexxx888

Alexxx888

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 September 2012 - 08:02 AM

Hi guys! I am new to Python language and i was told to do the following thing:

I`ll have a CSV file with something like
Class,some value,name,some value,grade,some value
Class,some value,name,some value,grade,some value

And so on. What i have to do is to find the average grade. Also if for some reason the document is now longer like:

Class,some value,name,some value,grade,some value

but it gets like this:

grade,some value,name,some value,Class,some value

The program should still find the appropriate field and the coresponding value. So if i open the document like that:

import csv

file_handle = open('a.csv','r')
reader = csv.reader(file_handle, delimiter=',', quotechar='|')

And try to call row[0] it give an error, but if i do the following

for row in reader:
print(','.join(row))

it gives no error and everything is fine. When i call row[0] it prints the first value of the last row which means that when i tried to call it in the begging i had no "pointer" to the first row. So far i cant get the seek to work with reader. It gives me that reader is not a csv.reader something although it should be.
Is there a way to itterate each value in a row until i find "grade" and the sum row[x+1]?
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 29 September 2012 - 08:20 AM

I can't see you read out any rows from the reader?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 Alexxx888

Alexxx888

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 September 2012 - 08:50 AM

Eh? :D . Something like

  • for row in reader:
  • for item in row
  • print item but when i try to execute for row in reader: twice the second time i get nothing...

  • 0

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 29 September 2012 - 08:57 AM

I don't know reader in python, but yes, something like that. but I don't think you want to do for item in row, as you only want specific items directly...
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#5 Alexxx888

Alexxx888

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 September 2012 - 09:06 AM

reader = csv.reader(file_handle, delimiter=',', quotechar='|') where file_handle = open('a.csv','r') wher a.csv is my file
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download