Please help for Python programming

bruno modulix onurb at xiludom.gro
Tue Mar 22 04:45:49 EST 2005


yy0127 at gmail.com wrote:
(snip)
> The PRINT code is for my testing. My problem is bytemsg will be omitted
> some records. For example, my text file have 53 records about username.
> After byteDict = users1[user1], 

Which, from your previous snippet, should raise a KeyError... If it 
does, then first understand why and fix it. If it does not, then the 
code you posted is absolutely useless for us to help you.

> it remains 50 records in the output
> file.

There is nothing about reading and parsing file in the code you posted.

I'm afraid you did not follow Diez's advice, nor mine. Please re-read my 
previous post, take appropriate action, and re-post with the minimum 
*working* snippet that exhibit your problem.


> I would like to know how to culmulate some data by users.

---- data.txt
user1;aaa;000
user2;aab;001
user3;aac;002
user1;aad;004
user3;aae;005
user1;aaf;006
user2;aag;007
user2;aah;008
user2;aak;009
user1;zzz;999


---- accu.py
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 = {}
for line in f:
   try:
      user, data1, data2 = line.strip().split(';')
   except ValueError:
      print >> sys.stderr, "wrong file format"
      f.close()
      sys.exit(1)
   try:
     users[user].append("%s : %s" % (data1, data2))
   except KeyError:
     users[user] = ["%s : %s" % (data1, data2)]

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

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for 
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list