Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python½Å±¾½âÎöBitTorrentÖÖ×ÓÎļþÄÚÈÝ

ÓкܶàÖÖ×ÓÎļþ£¬ÓÐʱºò¼Ç²»ÇåÀïÃ涼ÊÇʲô¶«Î÷£¬ÓÖ²»ÏëÒ»¸öÒ»¸öµÄÍϷŵ½Ñ¸À×»òBTÈí¼þÀïÍ·¿´£¬
ÉÏÍø²éÁËÒ»ÏÂPythonµÄ½Å±¾£¬×Ô¼ºÒ²ÉÔ΢ÐÞ¸ÄÁËһϣ¬´úÂëÈçÏ£¬Õ³Ìùµ½Îı¾±à¼­Æ÷ÖУ¨×îºÃÊÇ´ø¸ñʽµÄÈçUltraEdit»òVS2008µÈ£©£º
±£´æ³Épyºó׺µÄ£¬Ö±½ÓÔËÐÐ
import re
def tokenize(text, match=re.compile("([idel])|(\d+):|(-?\d+)").match):
i = 0
while i < len(text):
m = match(text, i)
s = m.group(m.lastindex)
i = m.end()
if m.lastindex == 2:
yield "s"
yield text[i:i+int(s)]
i = i + int(s)
else:
yield s
def decode_item(next, token):
if token == "i":
# integer: "i" value "e"
data = int(next())
if next() != "e":
raise ValueError
elif token == "s":
# string: "s" value (virtual tokens)
data = next()
elif token == "l" or token == "d":
# container: "l" (or "d") values "e"
data = []
tok = next()
while tok != "e":
data.append(decode_item(next, tok))
tok = next()
if token == "d":
data = dict(zip(data[0::2], data[1::2]))
else:
raise ValueError
return data
def decode(text):
try:
src = tokenize(text)
data = decode_item(src.next, src.next())
for token in src: # look for more tokens
raise SyntaxError("trailing junk")
except (AttributeError, ValueError, StopIteration):
raise SyntaxError("syntax error")
return data
if __name__ == "__main__":
#ÐèÒª¶ÁÈ¡µÄÎļþÃû³Æ·Åµ½ÕâÀï
data = open("Riko.Tachibana.torrent", "rb").read()
torrent = decode(data)
myfile = file("testit.txt", 'w')
a = u'ÎļþÃû³Æ'.encode('gbk')
b = u'Îļþ´óС'.encode('gbk')
print "%s \t %s \n" % (a,b)
for file in torrent["info"]["files"]:
print "%s \t %d Mb " % ("/".join(file["path"]), file["length"]/1024/1024)
print "------------------------------


Ïà¹ØÎĵµ£º

Python×Ö·û´®µÄencodeÓëdecode

 
Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö· ......

PythonµÄ´®¿ÚͨѶµÚÈý·½¿âPySerial

 ×î½üÒªÓõ½´®¿ÚͨѶ£¬¼òµ¥Ò×ÓõÄPythonÓÖ°ïÉÏæÁË£¬¶à¿÷ÁËÅÓ´óµÄµÚÈý·½×ÊÔ´~~~ £º£©
pySerial
Overview
This module encapsulates the access for the serial port. It provides
backends for Python running on Windows, Linux, BSD (possibly any POSIX
compliant system), Jython and IronPython (.NET and M ......

Google½«ÏÞÖÆPythonÓïÑÔµÄÓ¦Óà ¿ª·¢ÉçÇøÈÈÒé

Google½«ÏÞÖÆPythonÓïÑÔµÄÓ¦Óà ¿ª·¢ÉçÇøÈÈÒé
 
¸ÐлÂ̲ÝÐÇ¿ÕµÄͶµÝ
ÐÂÎÅÀ´Ô´:CSDN
Collin WinterÊÇPythonÉçÇøһλÆľßÓ°ÏìÁ¦µÄ¿ª·¢Õߣ¬ËûÔøÊÇCPythonÏîÄ¿µÄºËÐÄ¿ª·¢ÕßÖ®Ò»¡¢Ò²ÔøÊÇUnladen Swallow£¨¼ûÎÄĩעÊÍ£©µÄºËÐÄ¿ª·¢Õߣ¬²ÎÓëÁ˺ܶàPythonÏîÄ¿µÄ¿ª·¢¡£½üÀ´´«ÎÅGoogle½«ÔÚÆäÐÂÏîÄ¿ÖÐÏÞÖÆPythonµÄʹÓã¬Îª´ËÓп ......

¶ÔpythonµÄisinstanceµÄÈÏʶ

>>> class objA:
...     pass
...
>>> A = objA()
>>> B = 'a','V'
>>> B
('a', 'V')
>>> C = 'a string'
>>> print instance(A,objA)
Traceback (most recent call last):
  File "<stdin>", line 1, in < ......

Python×Ö·û´®µÄencodeÓëdecode


Ê×ÏÈÒª¸ãÇå³þ£¬×Ö·û´®ÔÚPythonÄÚ²¿µÄ±íʾÊÇunicode±àÂ룬Òò´Ë£¬ÔÚ×ö±àÂëת»»Ê±£¬Í¨³£ÐèÒªÒÔunicode×÷ΪÖмä±àÂ룬¼´ÏȽ«ÆäËû±àÂëµÄ×Ö·û´®½âÂ루decode£©³Éunicode£¬ÔÙ´Óunicode±àÂ루encode£©³ÉÁíÒ»ÖÖ±àÂë¡£
decodeµÄ×÷ÓÃÊǽ«ÆäËû±àÂëµÄ×Ö·û´®×ª»»³Éunicode±àÂ룬Èçstr1.decode('gb2312')£¬±íʾ½«gb2312±àÂëµÄ×Ö·û´®×ª» ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ