Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 108 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_: .. image:: http://procasts.co.uk/media/procasts_sma ...

Writing the new .csv file with an average [ID:257] (16/17)

in series: Python 101 - easygui and csv

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Now we're in a position to write out a new .csv file (using the csv module) which can be read by Excel. We start with a simple .csv file which looks identical to the one we read in, then we add the new row for the averaged values.

We go on to use Excel's '=average' functions to verify that we have indeed calculated the right output.

Created May 2007, running time 7 minutes

# Two files here

#
# 910150_csvReader.py
#
import csv
import sys
import easygui

def getFileAndPath():
    "Get fully-qualified path to the csv file"
    # TODO argInitialFile = '*.csv'
    fileAndPath = easygui.fileopenbox(title="Select .CSV file")
    print "Using:",fileAndPath 
    return fileAndPath

def getLinesInCSVFile(fileAndPath):
    "read lines in CSV file, return a list of these lines"
    linesInCSV = []
    reader = csv.reader(open(fileAndPath, "rb"))
    for row in reader:
        linesInCSV.append(row)
    return linesInCSV

def justNumbers(listOfStrings):
    "True if the list contains just numbers represented as strings"
    # e.g. ['22.4', '23.9']
    isJustNumbers = True
    for item in listOfStrings:        
        try:
            nbr = float(item)            
        except ValueError:
            isJustNumbers = False
    return isJustNumbers

def getNumbers(listOfStrings):
    "Convert a list of strings-of-numbers to a list of numbers, e.g. ['22.4', '23.9'] -> [22.4, 23.9]"
    numbers = []
    for item in listOfStrings:
        nbr = float(item)            
        numbers.append(nbr)
    return numbers

def average(values):
    """Computes the arithmetic mean of a list of numbers"""
    return sum(values, 0.0) / len(values)

if __name__ == "__main__":
    # get the file-name
    #fileAndPath = getFileAndPath()
    # NOTE quick hack to make our test/development process quicker
    fileAndPath = "C:/LearningPython_CSV/measured.csv"
        
    # read the CSV file
    linesInCSV = getLinesInCSVFile(fileAndPath)
        
    measured1 = []
    measured2 = []
    for n in range(1,4):
        line = linesInCSV[n]
        isJustNumbers = justNumbers(line)
        if not isJustNumbers:
            print "ERROR!  Expected a line of numbers, instead we got:",line
            sys.exit() 
        # we only get here if justNumbers reports that we only have numbers
        # so we can extract the list of floating-point numbers
        numbers = getNumbers(line)
        measured1.append(numbers[0])
        measured2.append(numbers[1])
    averaged1 = average(measured1)
    averaged2 = average(measured2)
    
    # Show values of Measured1 in a choicebox
    # We don't care about the choices, this is just for output
    #easygui.choicebox(message = "Sorted values in Measured1", title = "Measured1", choices = measured1)

    headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2']
    csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", argInitialFile='*.csv')
    if csvOutFileName is not None:    
        print "Saving using: "+csvOutFileName
        csvOut = file(csvOutFileName, 'wb')
        writer = csv.writer(csvOut)
        writer.writerow(headings)
        for index in range(len(measured1)):
            writer.writerow([measured1[index], measured2[index]])
        writer.writerow([averaged1, averaged2])
    else:
        print "No filename for saving"
        

#
# 910150_testCsvReader.py
#
import csvReader

def testJustNumbersOnNumbers():
    listOfStrings = ['22.4', '23.9']
    assert csvReader.justNumbers(listOfStrings)
    
def testJustNumbersOnBadNumbers():
    listOfStrings = ['abc', '23.9']
    assert csvReader.justNumbers(listOfStrings) == False

def testGetNumbers():
    listOfStrings = ['22.4', '23.9']
    assert csvReader.getNumbers(listOfStrings) == [22.4, 23.9]

def testAverage():
    assert csvReader.average([1,2,3]) == 2.0
    

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 159 (since July 30th)
  • Plays in last week: 0
  • Published: 91 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

Writing the new .csv file with an average (16/17)

2:00 filesavebox returns None if user presses cancel

3:46 suggested pause point for placement of index

4:46 suggested pause point for placement of measure2

5:53 suggested pause point for output of averages


Hi Ian,

Very nice video!. I particularly liked the reference back to Excel when you recalculate the averages. It should encourage more people to tame Excel via Python.

Jerol


Thanks for putting this out. It beats pouring over books (and yes I've got a small library of Python books). This stuff is starting to make some sense now.

Cheers!


Review of Writing the new .csv file with an average

Hi Ian,

This was really useful as I need to do tests on some csv files.

Greatly appreciated

Thanks


Review of Writing the new .csv file with an average

This series is good. I like the approch of giving little TODO exercises.


What you're looking at in the source display above is *2* files, not one. You need to copy this source and then split them into two files (csvReader.py, testCsvReader.py).

I don't show this in the video, it ought to be clearer.

Once testCsvReader.py is referring to 'import csvReader' and csvReader.py is a *separate* file, it'll all work.

Ian.


Great Video (as per usual high standard), however why does Boa tell me this? It all seems to work though

Traceback (most recent call last):

File "csvReader.py", line 94, in <module>

import csvReader

ImportError: No module named csvReader


Excellent. This is exactly the kind of data cruching I wanted ot learn with Python - everything is coming together.


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com