Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HW help for a noob
#1
Rather confused with what I'm doing wrong. First CS course and I'm stuck on this part. I need to import the CSV file from excel named "data.csv", but I have zero clue as to why this code is wrong. Thanks for your help. Below is the code.



import csv
path = data.csv
def mean(ch):
A = list()
file = open(path, newline="")
reader1 = csv.reader(file)
next(reader1)
for row in reader1:

A.append(int(row[ch-1]))
return(sum(A)/len(A))

The error is "Unidentified name 'data'"
Reply
#2
please read BBCODE
and post code between code tags, use shift-ctrl-v to preserve indentation.

see: https://docs.python.org/3/library/csv.html for info on reading/ writing csv files
Reply
#3
Also post the error code in it's entirety between the error tags.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
You need to put the name of the file in quotes.

You need to call the function you defined.

I assume the ch argument is meant to indicate which column (field) of data you want to get the mean of, counting from 1 (hence the ch-1 referencing as Python counts from 0).

import csv
path = 'data.csv'
def mean(ch):
    A = list()
    file = open(path, newline="")
    reader1 = csv.reader(file)
    next(reader1)
    for row in reader1:
        A.append(int(row[ch-1]))
    return(sum(A)/len(A))
    
print(mean(1))
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020