Please help for Python programming

Discussion in 'Python' started by yy0127@gmail.com, Mar 21, 2005.

  1. Guest

    I don't know why i entered the below code and it will miss some
    records.
    Anyone can help me???

    users = {}
    users1 = {}


    while 1:
    user, serviceType, msgType, inOut, date, time, numBytes =
    aLog.GetNextMessage("")
    fullmsg = serviceType + "|" + msgType + "|" + inOut
    bytemsg = user + " " + serviceType + " " + msgType + " " + inOut + " "
    + numBytes

    user1 = user

    msgDict = {}
    byteDict = {}

    print bytemsg # 53 records in source file
    msgDict = users[user] # get the cum statistics for this user
    byteDict = users1[user1]
    print bytemsg # 50 records in source file
     
    , Mar 21, 2005
    #1
    1. Advertising

  2. wrote:

    > I don't know why i entered the below code and it will miss some
    > records.
    > Anyone can help me???


    Sorry, my mindreading brain extension is at the service right now, so I
    can't guess what that piece of syntactically incorrect code is supposed to
    do and what error message it produces.

    Until you come up with a more detailed error description, I'll have a good
    read at

    http://www.catb.org/~esr/faqs/smart-questions.html


    Which I suggest you read too.

    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Mar 21, 2005
    #2
    1. Advertising

  3. wrote:
    > I don't know why i entered the below code


    And we don't know either !-)

    > and it will miss some
    > records.
    > Anyone can help me???


    If you hope to get some useful help, it would be a good idea to follow
    Diez's advice (see other post in this thread)

    > users = {}
    > users1 = {}
    >
    > while 1:


    Unless there is a break somewhere in the following code (I can't find
    one...), this is an endless loop. But you know this, don't you ?

    > user, serviceType, msgType, inOut, date, time, numBytes =
    > aLog.GetNextMessage("")
    > fullmsg = serviceType + "|" + msgType + "|" + inOut

    tip : use string formating instead (string concatenations are to be
    avoided in Python), ie:
    fullmsg = "%s | %s | %s" % (serviceType, msgType, inOut)

    > bytemsg = user + " " + serviceType + " " + msgType + " " + inOut + " "
    > + numBytes

    idem

    > user1 = user
    >
    > msgDict = {}
    > byteDict = {}


    This re-initialises both dicts on each iteration. Is this really what
    you want ?
    >
    > print bytemsg # 53 records in source file


    Here you have an obvious indentation problem. This code *cannot* run.
    Take care of not mixing tabs and spaces (tip: configure you editor to
    only use spaces)

    > msgDict = users[user] # get the cum statistics for this user

    Given the above binding of 'users' as an empty dict, this should raise a
    KeyError. It's also overwriting the previous binding of msgDict.

    > byteDict = users1[user1]


    idem. Also, you don't need to bind 2 different names to the same value
    to use this value as key in 2 different dicts. Here you could as well
    use user for both dicts, since user and user1 are bound to the same value.

    > print bytemsg # 50 records in source file
    >


    In addition to Diez's reading advice, here are some that are more
    specific to code-related questions:

    1/ paste code, dont re-type it
    .... this avoid stupid typos

    2/ post running code
    .... if the code is so obviously broked that it cannot even compile|run,
    readers will have to fix it first - which they'll probably won't do.
    Even if they do, they may not fix it the right way. Everyone's losing
    its time...

    3/ post the smallest possible bit of code that exhibit your problem
    .... no one's going to [read 3000 lines of code | install 30 gigabytes of
    third part libs | etc...] just to help you. Moreover, quite often, one
    finds the bug while reducing the problematic code to it's smallest possible.

    And one last: don't forget to put your bullet-proof jacket on before
    reading the answers !-) (well, c.l.py is probably one of the friendliest
    groups on usenet, but still, this is usenet).


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Mar 21, 2005
    #3
  4. Terry Reedy Guest

    This is what I see (minus the '> '):

    > while 1:
    > user, serviceType, msgType, inOut, date, time, numBytes =
    > aLog.GetNextMessage("")

    [etc]

    Advice: use spaces, not tabs, to indent posted code (some readers discard
    tabs). Don't use google groups to post code (it deletes initial spaces and
    won't, apparently, fix this bug). Use news.gmane.org group
    gmane.comp.python.genral instead (I believe they also have web interface in
    addition to newsreader interface). If you really, really must post thru
    google, prefix lines with char such as '|', even tho this kill cut and
    pastability.

    Terry J. Reedy
     
    Terry Reedy, Mar 21, 2005
    #4
  5. Guest

    Actually, this script will open a text file which is seperated by tabs.


    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], it remains 50 records in the output
    file.

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

    Thanks!
     
    , Mar 22, 2005
    #5
  6. 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 ''.split('@')])"
     
    bruno modulix, Mar 22, 2005
    #6
  7. M.E.Farmer Guest

    Terry,
    This was posted from google groups, can you see the indents?
    # code snippet
    convertpage = 0
    form = None
    for o, a in opts:
    if o in ["-h", "--help"]:
    Usage()
    sys.exit()
    if o in ["-o", "--output", "--out"]:
    output = a
    if o in ["-i", "--input", "--in"]:
    input = a
    if input in [".", "cwd"]:
    input = os.getcwd()

    Notice the 'fixed font / proportional font' link in the top right
    corner.
    I think they have "fixed" the problem.
    M.E.Farmer

    Terry Reedy wrote:
    > This is what I see (minus the '> '):
    >
    > > while 1:
    > > user, serviceType, msgType, inOut, date, time, numBytes =
    > > aLog.GetNextMessage("")

    > [etc]
    >
    > Advice: use spaces, not tabs, to indent posted code (some readers

    discard
    > tabs). Don't use google groups to post code (it deletes initial

    spaces and
    > won't, apparently, fix this bug). Use news.gmane.org group
    > gmane.comp.python.genral instead (I believe they also have web

    interface in
    > addition to newsreader interface). If you really, really must post

    thru
    > google, prefix lines with char such as '|', even tho this kill cut

    and
    > pastability.
    >
    > Terry J. Reedy
     
    M.E.Farmer, Mar 22, 2005
    #7
  8. Terry Reedy Guest

    "M.E.Farmer" <> wrote in message
    news:...
    > Terry,
    > This was posted from google groups, can you see the indents?


    Yes, looks good

    > # code snippet
    > convertpage = 0
    > form = None
    > for o, a in opts:
    > if o in ["-h", "--help"]:
    > Usage()
    > sys.exit()
    > if o in ["-o", "--output", "--out"]:
    > output = a
    > if o in ["-i", "--input", "--in"]:
    > input = a
    > if input in [".", "cwd"]:
    > input = os.getcwd()
    >
    > Notice the 'fixed font / proportional font' link in the top right
    > corner.


    I presume this is on the Google page.

    > I think they have "fixed" the problem.


    Great. Now people have to learn to use the fixed font choice.

    TJR
     
    Terry Reedy, Mar 22, 2005
    #8
  9. M.E.Farmer Guest

    Hello again,
    For some strange reason Google isn't showing this so I got this from
    gmane sorry if I missed something.
    The fixed/proportional link is located on the google groups c.l.py
    pages.
    I wasn't clear at all sorry for the ambiguity.
    Google groups for c.l.py seems to be fixed by default, so no need to
    click it.
    Proportional still retains spaces but is less tidy.
    M.E.Farmer
    >> Terry,
    >> This was posted from google groups, can you see the indents?


    >Yes, looks good


    >> # code snippet
    >> convertpage = 0
    >> form = None
    >> for o, a in opts:
    >> if o in ["-h", "--help"]:
    >> Usage()
    >> sys.exit()
    >> if o in ["-o", "--output", "--out"]:
    >> output = a
    >> if o in ["-i", "--input", "--in"]:
    >> input = a
    >> if input in [".", "cwd"]:
    >> input = os.getcwd()
    >>
    >> Notice the 'fixed font / proportional font' link in the top right
    >> corner.


    >I presume this is on the Google page.


    >> I think they have "fixed" the problem.


    >Great. Now people have to learn to use the fixed font choice.


    >TJR
     
    M.E.Farmer, Mar 23, 2005
    #9
  10. Guest

    Thanks for your advice.^^

    I find out something special (because i don't know). I would like to
    retrieve data from source file into array. The data will be omited if
    the value is duplicated. For example, there is 3 values - 1,2,1. It
    will only display 1,2 after i ran the script.

    value = (1,2,1)
    while 1:
    users = {}
    byteDict = {}

    byteDict = users[value]

    users[value] = byteDict
    print users

    I think the above code cannot run properly. Any ppl knows why the value
    omited when it was duplicated.

    Many of thanks for all!!!
     
    , Mar 23, 2005
    #10
  11. wrote:
    > Thanks for your advice.^^


    It would be more useful to follow them than to thank me.

    > I find out something special (because i don't know). I would like to
    > retrieve data from source file into array. The data will be omited if
    > the value is duplicated. For example, there is 3 values - 1,2,1. It
    > will only display 1,2 after i ran the script.
    >
    > value = (1,2,1)
    > while 1:
    > users = {}
    > byteDict = {}
    > byteDict = users[value]
    > users[value] = byteDict
    > print users
    >
    > I think the above code cannot run properly.


    Don't think. Be sure. Verify. Try to find out why it cannot run at
    all[1]. Fix this problem first. *Then* we'll happily read your next
    posts and try to help you fix the other problems if there are...

    [1] It's pretty obvious for anyone having read any python beginner
    tutorial, I already explained you why, and gave you a working example.

    If you don't read answers, don't post questions :-/

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Mar 23, 2005
    #11
  12. Guest

    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!!!
     
    , Mar 24, 2005
    #12
  13. wrote:
    > 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.


    This is (almost) exactly the same.

    > 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


    Q1 : What do you think this will do ?

    > except KeyError:
    > users[user] = ["%s : %s" % (data1, data2)]
    > cumdata3 = int(data2) + cumdata3


    Q2 : What do you think this will do ?

    > f.close()
    > print "collected data:"
    > pprint.pprint(users)
    > print cumdata2
    > print cumdata3
    > ##
    >
    > The above example can run but the total num are wrong.


    First, please post your test data set, the expected result and the
    actual result. Saying 'the total num is wrong' doesn't give a clue.

    > Would you mind
    > to figure out where is my problem?


    I may be wrong (pun intended), but I think you don't really understand
    what this code is doing, specially this part :

    try:
    users[user].append(something)
    except KeyError:
    users[user] = [something]

    In fact I think your problem is that you still have not read the fine
    manual, specially the part about dicts.

    Read the manual, understand the above snippet - which is a pretty common
    idiom in Python -, try to answer Q1 and Q2, and you should be able to
    work it out by yourself.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno modulix, Mar 24, 2005
    #13
  14. Guest

    Thanks for your help! I am already fix it!!

    Many thanks!
     
    , Mar 30, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    541
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,785
    Mike Hewson
    Jan 7, 2005
  4. bwaha
    Replies:
    12
    Views:
    782
    bwaha
    Jan 11, 2006
  5. mera
    Replies:
    7
    Views:
    1,130
Loading...

Share This Page