请帮助Python编程 [英] Please help for Python programming

查看:52
本文介绍了请帮助Python编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我输入以下代码,它会遗漏一些

记录。

任何人都可以帮助我???


users = {}

users1 = {}

而1:

user,serviceType,msgType,inOut, date,time,numBytes =

aLog.GetNextMessage("")

fullmsg = serviceType +" |" + msgType +" |" + inOut

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

+ numBytes


user1 =用户


msgDict = {}

byteDict = {}


print bytemsg#53源文件中的记录

msgDict = users [user]#获取此用户的暨统计信息

byteDict = users1 [user1]

print bytemsg#源文件中的50条记录

解决方案

< blockquote> yy****@gmail.com 写道:

我不知道我不知道为什么我输入下面的代码,它会遗漏一些
记录。
任何人都可以帮助我???




对不起,我的思维大脑扩展现在正处于服务状态,所以我不能猜测这句语法不正确的代码应该是什么,以及它产生的错误信息。


在你想出更详细的错误描述之前,我会有一个很好的

阅读

<一个rel =没有按照href =http://www.catb.org/~esr/faqs/smart-questions.htmltarget =_ blank> http://www.catb.org/~esr/faqs/smart-questions .html

我建议您也阅读。


-

问候,


Diez B. Roggisch


yy **** @ gmail.com 写道:

我不知道为什么我输入以下代码


我们也不知道! - )

它会遗漏一些
记录。
任何人都可以帮助我???


如果你希望获得一些有用的帮助,那么按照

Diez的建议是个好主意(参见本篇帖子中的其他文章)

users = {}
users1 = {}

1:


除非在某处有休息下面的代码(我找不到

一个...),这是一个无限循环。但你知道吗,不是吗?

user,serviceType,msgType,inOut,date,time,numBytes =
aLog.GetNextMessage("")
fullmsg = serviceType +" |" + msgType +" |" + inOut
提示:使用字符串格式化(字符串连接是在Python中避免使用
),即:

fullmsg ="%s | %s | %S" %(serviceType,msgType,inOut)

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

user1 = user

msgDict = {}
byteDict = {}


这会在每次迭代时重新初始化两个dicts。这真的是你想要的吗?
打印bytemsg#53源文件中的记录


这里有一个明显的缩进问题。此代码*无法*运行。

注意不要混合标签和空格(提示:配置编辑器

仅使用空格)

msgDict = users [user]#获取此用户的暨统计信息
鉴于上述用户绑定为空dict,这应该会引发一个

KeyError。它还覆盖了之前msgDict的绑定。

byteDict = users1 [user1]


同上。此外,您不需要将2个不同的名称绑定到相同的值

,以将此值用作2个不同的词组中的键。在这里你也可以使用两个dicts的用户,因为user和user1绑定了相同的值。

print bytemsg#源文件中的50条记录




除了Diez的阅读建议之外,以下是一些针对代码相关问题的更多



1 /粘贴代码,不要重新输入它

....这可以避免愚蠢的拼写错误


2 /发布运行代码

....如果代码显然被破坏甚至无法编译|

读者必须先修复它 - 它们可能会赢得'' t。

即使他们这样做,也可能无法以正确的方式解决问题。每个人都在失去

时间......


3 /发布可能出现问题的最小代码

....没有人去[阅读3000行代码|安装30千兆字节

第三部分库|等......只是为了帮助你。而且,通常情况下,一个

发现错误,同时将有问题的代码减少到最小可能。


最后一个:不要忘记穿上你的防弹夹克了

阅读答案! - )(好吧,clpy可能是usenet上最友好的

组之一,但是,这是usenet)。

-

bruno desthuilliers

python -c" print''@''。join(['' 。''。加入([w [:: - 1] for p in p.split(''。'')])for

p in''o****@xiludom.gro ''.split(''@'')])"


这就是我所看到的(减去''>''):

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



[etc]


建议:使用空格而不是制表符缩进发布的代码(有些读者丢弃

制表符)。不要使用谷歌小组发布代码(它删除初始空格和

不会,显然,修复此错误)。请使用news.gmane.org组

gmane.comp.python.genral(我相信他们在新闻阅读器界面中除了
之外还有网页界面)。如果你真的,真的必须发布通过

谷歌,用'|''这样的字符加前缀行,甚至这个杀戮和

的可用性。


Terry J. Reedy


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

解决方案

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


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(''@'')])"


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


这篇关于请帮助Python编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆