Week 14

More About Applications and More Review

Author:Christopher Haynes
Email:chaynes@indiana.edu
Affiliation:Indiana University
Course:BL CSCI A201
Date:2008-04-18
Copyright © 2008, Christopher Haynes—all rights reserved.

CSV file exercise (from last week)

Write a function csv_column that takes the name of a CSV file and a column number (one-based indexing) and prints the elements in the column.

Recall: list(csv.reader(csv_file_object)) returns table representing the contents of the CSV file.

CSV Column Application Exercise

Extend the solution from last week to create an application that prompts for the input file and column number, and then invokes this function.

Repeat the column number prompt if a positive integer is not entered. Recall the string method isdigit().

import csv

def csv_column(csv_file_name, column_number):
    """Print the strings in the indicated column (one-based indexing) in
    the named CSV file, excluding empty strings.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            print value

CSV Column Application Solution

import csv

def csv_column(csv_file_name, column_number):
    """Print the strings in the indicated column (one-based indexing) in
    the named CSV file, excluding empty strings.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            print value

def main():
    """Prompt for csv file name and column number, and then call
    the csv_column function. Repeat the column number prompt if a positive
    integer is not entered.
    """
    file_name = raw_input('CSV file name: ')
    while True:
        column = raw_input('Column number: ')
        if column.isdigit():
            column_number = int(column)
            if column_number > 0:
                break
    csv_column(file_name, column_number)

main()

OS shell command line arguments

OS shell command arguments and Python

CSV Column Application with command line arguments

import csv, sys

def csv_column(csv_file_name, column_number):
    """Print the strings in the indicated column (one-based indexing) in
    the named CSV file, excluding empty strings.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            print value

def main():
    """Usage: python csv_column3.py <csv file name> <column number>
    """
    if len(sys.argv) == 3:
        csv_file_name = sys.argv[1]
        column = sys.argv[2]
        if column.isdigit():
            column_number = int(column)
            if column_number > 0:
                csv_column(csv_file_name, column_number)
                return
        print "Error: bad column number"
    else:
        print main.__doc__ # print the function documentation string

main()

Output file argument

We add an output file name argument to our application, so output goes to a file rather than being printed.

import csv, sys

def csv_column(csv_file_name, column_number, output_file_name):
    """Write the strings in the indicated column (one-based indexing) in
    the named CSV file, one per line, excluding empty strings. Write to
    the named output file.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()

    output_file = file(output_file_name, 'w')
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            output_file.write(value + '\n')
    output_file.close()

def main():
    """Usage: python csv_column4.py <csv file name> <column number> <output file>
    """
    if len(sys.argv) == 4:
        csv_file_name = sys.argv[1]
        column = sys.argv[2]
        output_file_name = sys.argv[3]
        if column.isdigit():
            column_number = int(column)
            if column_number > 0:
                csv_column(csv_file_name, column_number, output_file_name)
                return
        print "Error: bad column number"
    else:
        print main.__doc__ # print the function documentation string

main()

clicker Which of the following is not an expression (in Python)?

  1. a[3]
  2. a[3, 4]
  3. a(3,4)
  4. a[3:4]
  5. a(3)
  6. none of the above: they are all expressions

Answer: B

clicker Which of the following is not an expression in Python?

  1. [3]
  2. [3, 4]
  3. [3:4]
  4. (3)
  5. none of the above: they are all expressions

Answer: C

clicker How much do you think Alice helped you learn programming?

  1. A lot
  2. A bit
  3. Very little
  4. Not at all

clicker Continue using Alice?

Do you think it would it be more helpful for most students to not use Alice?

The course would then probably start with a bit more Reeborg programming, a little slower pace with the current Python material, and perhaps some Python graphics programming at the end of the course.
  1. Yes
  2. Maybe so
  3. Probably not
  4. No

FYI: Standard output

Standard output example

import csv, sys

def csv_column(csv_file_name, column_number, output_file_name):
    """Write the strings in the indicated column (one-based indexing) in
    the named CSV file, one per line, excluding empty strings. Write to
    the named output file.
    """
    # Hint: list(csv.reader(<file object>))
    csv_file = file(csv_file_name, 'r')
    table = list(csv.reader(csv_file))
    csv_file.close()

    output_file = file(output_file_name, 'w')
    column_number = column_number - 1
    for index in range(len(table)):
        row = table[index]
        value = row[column_number]
        if value.strip() != '':
            output_file.write(value + '\n')
    output_file.close()

def main():
    """Usage: python csv_column4.py <csv file name> <column number> <output file>
    """
    if len(sys.argv) == 4:
        csv_file_name = sys.argv[1]
        column = sys.argv[2]
        output_file_name = sys.argv[3]
        if column.isdigit():
            column_number = int(column)
            if column_number > 0:
                csv_column(csv_file_name, column_number, output_file_name)
                return
        print "Error: bad column number"
    else:
        print main.__doc__ # print the function documentation string

main()

FYI Raising and handling exceptions