SimplePrograms challenge

infidel saint.infidel at gmail.com
Wed Jun 13 17:34:57 EDT 2007


# writing/reading CSV files, tuple-unpacking, cmp() built-in
import csv

writer = csv.writer(open('stocks.csv', 'wb'))
writer.writerows([
    ('GOOG', 'Google, Inc.', 505.24, 0.47, 0.09),
    ('YHOO', 'Yahoo! Inc.', 27.38, 0.33, 1.22),
    ('CNET', 'CNET Networks, Inc.', 8.62, -0.13, -1.49)
])

stocks = csv.reader(open('stocks.csv', 'rb'))
for ticker, name, price, change, pct in stocks:
    print '%s is %s (%s%%)' % (
        name,
        {-1: 'down', 0: 'unchanged', 1: 'up'}[cmp(float(change),
0.0)],
        pct
    )




More information about the Python-list mailing list