473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Please help for Python programming

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

Jul 18 '05 #1
13 1566
yy****@gmail.com 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
Jul 18 '05 #2
yy****@gmail.com 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 'o****@xiludom.gro'.split('@')])"
Jul 18 '05 #3
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

Jul 18 '05 #4
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!

Jul 18 '05 #5
yy****@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 'o****@xiludom.gro'.split('@')])"
Jul 18 '05 #6
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


Jul 18 '05 #7

"M.E.Farmer" <me*****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Jul 18 '05 #8
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


Jul 18 '05 #9
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!!!

Jul 18 '05 #10
yy****@gmail.com 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 'o****@xiludom.gro'.split('@')])"
Jul 18 '05 #11
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!!!

Jul 18 '05 #12
yy****@gmail.com 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 'o****@xiludom.gro'.split('@')])"
Jul 18 '05 #13
Thanks for your help! I am already fix it!!

Many thanks!

Jul 18 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: kbass | last post by:
In different articles that I have read, persons have constantly eluded to the productivity gains of Python. One person stated that Python's productivity gain was 5 to 10 times over Java in some in...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
10
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
35
by: John Coleman | last post by:
Greetings, I have a rough classification of languages into 2 classes: Zen languages and tool languages. A tool language is a language that is, well, a *tool* for programming a computer. C is the...
17
by: MilkmanDan | last post by:
I'll be a college freshman this fall, attending Florida Institute of Tech studying electrical engineering. I was considering taking some classes in programming and computer science, and I...
10
by: Jerry | last post by:
I have just started to do some semi-serious programming (not one-off specialized scripts) and am loving Python. I just seem to get most of the concepts, the structure, and the classes (something I...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
11
by: Monty Taylor | last post by:
Hey everybody, MySQL has put up a poll on http://dev.mysql.com asking what your primary programming language is. Even if you don't use MySQL - please go stick in a vote for Python. I'm...
28
by: Hussein B | last post by:
Hey, I'm a Java/Java EE developer and I'm playing with Python these days. I like the Python language so much and I like its communities and the Django framework. My friends are about to open a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.