| Author: | Christopher Haynes |
|---|---|
| Email: | chaynes@indiana.edu |
| Affiliation: | Indiana University |
| Course: | BL CSCI A201 |
| Date: | 2008-04-18 |
Contents
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.
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
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()
In Python, command-line arguments are available as a list of string in the variable argv of module sys
OS shell demo:
> type args.py import sys print sys.argv > python args.py arg1 "argument two" 3 ['args.py', 'arg1', 'argument two', '3']
FYI
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()
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()
Which of the following is not an expression (in Python)?Answer: B
Which of the following is not an expression in Python?Answer: C
How much do you think Alice helped you learn programming?
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.
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()