Please help for Python programming

yy0127 at gmail.com yy0127 at gmail.com
Wed Mar 23 22:01:41 EST 2005


I am sorry that i forgot to see the working example. Base on your
example, i can show the value without missing but got the other
problem. I would like to culmulate the value by users. I was rewrite
your example but cannot not work.

##
import sys
import pprint

try:
    f = open('data.txt', 'r')
except IOError, e:
    print >> sys.stderr, "Cannot open file data.txt for reading : %s"
%e
    sys.exit(1)

users = {}
cumdata2 = 0
cumdata3 = 0
for line in f:

    try:
        user, data1, data2 = line.strip().split('\t')
    except ValueError:
        print >> sys.stderr, "wrong file format"
        f.close()
        sys.exit(1)
    try:
        users[user].append("%s : %s" % (data1, data2))
        cumdata2 = int(data2) + cumdata2
    except KeyError:
        users[user] = ["%s : %s" % (data1, data2)]
        cumdata3 = int(data2) + cumdata3

f.close()
print "collected data:"
pprint.pprint(users)
print cumdata2
print cumdata3
##

The above example can run but the total num are wrong. Would you mind
to figure out where is my problem?

Thanks for your prompt help!!!




More information about the Python-list mailing list