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

python enumerateÓ÷¨

python cookbook  
Recipe 2.5. Counting Lines in a File
£¬
     ½ñÈÕ·¢ÏÖÒ»¸öк¯Êý
enumerate
¡£Ò»°ãÇé¿ö϶ÔÒ»¸öÁбí»òÊý×é¼ÈÒª±éÀúË÷ÒýÓÖÒª±éÀúÔªËØÊ±£¬»áÕâÑùд£º

for
i
in
range
(0
,
len
(list
)):

    print
i
,
list
[
i
]
 
µ«ÊÇÕâÖÖ·½·¨ÓÐЩÀÛ׸£¬Ê¹ÓÃ
ÄÚÖÃenumerrateº¯Êý»áÓиü¼ÓÖ±½Ó£¬ÓÅÃÀµÄ×ö·¨£¬ÏÈ¿´¿´enumerateµÄ¶¨Ò壺

 
def
enumerate
(collection
):

    'Generates an indexed series: 
(0,coll[0]), (1,coll[1]) ...'
     
     i
=
0

     it
=
iter
(collection
)
     while
1
:

     yield
(i
,
it
.
next
())

     i
+=
1
 
enumerate»á½«Êý×é»òÁбí×é³ÉÒ»¸öË÷ÒýÐòÁС£Ê¹ÎÒÃÇÔÙ»ñÈ¡Ë÷ÒýºÍË÷ÒýÄÚÈݵÄʱºò¸ü¼Ó·½±ãÈçÏ£º

for
index
£¬
text
in
enumerate
(list
)):

   print
index
,
text
   ÔÚ
cookbookÀï½éÉÜ£¬Èç¹ûÄãÒª¼ÆËãÎļþµÄÐÐÊý£¬¿ÉÒÔÕâÑùд£º

count
=
len
(open
(thefilepath
,

rU

).
readlines
())
Ç°ÃæÕâÖÖ·½·¨¼òµ¥£¬µ«ÊÇ¿ÉÄܱÈ
½ÏÂý£¬µ±Îļþ±È½Ï´óʱÉõÖÁ²»Äܹ¤×÷£¬ÏÂÃæÕâÖÖÑ­»·¶ÁÈ¡µÄ·½·¨¸üºÏÊÊЩ¡£

Count
=
-
1

For
count
,
line
in
enumerate
(open
(thefilepath
,

rU

))£º

    Pass
Count
+=
1


Ïà¹ØÎĵµ£º

python »ñÈ¡±¾»úip

>>> from socket import socket, SOCK_DGRAM, AF_INET
>>> s = socket(AF_INET, SOCK_DGRAM)
>>> s.connect(('google.com', 0))
>>> s.getsockname()
('192.168.1.113', 43711) 
Linux:
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = ......

¹ØÓÚPythonµÄÁбíappend£¨£©·µ»ØNone¶ÔÏó


ÔÚLearning Python 3rd ÀïµÚÈý²¿·ÖÁ·Ï°ÌâÓеÀÌ⣺
1.c ±àдfor Ñ­»·£¬¶ÔÓÚ×Ö·û´®S£¬·µ»ØÒ»¸öеÄÁÐ±í£¬ÆäÖаüº¬ÁË×Ö·û´®Ã¿¸ö×Ö·ûµÄASCIIÂë¡£
ÎÒµÄ˼·ÊÇ£º
for i in S:
    L = [ord(i) for i in S] #Áбí½âÎöµÄ˼·
ÎҵĴíÎó˼·£º
for i in S:
    L = L.append(ord(i))
Ìáʾ¿Õ¶ ......

python¶ÁȡĿ¼ÏÂÎļþ²¢Éú³ÉÈÕÖ¾

ºÜ³¤µÄÒ»¶Î´úÂ룬µ«ºÜÇå³þ¡£¹þ¹þ¡£
import os
from time import strftime
stamp=strftime("%Y-%m-%d %H:%M:%S")
logfile = 'F:\\test\\m-php-framework\\tmp\logs\\error_report.log'
path = 'F:\\test\\'
files = os.listdir(path)
bytes = 0
numfiles = 0
for f in files:
if f.startswith('t'): ......

Python ´¦Àíexcel²¢×ªÎªtable

ʹÓÃxlrd
À´
¶ÁÈ¡£¬xlrdµÄÏÂÔØ¼°°²×°¿ÉÒԲο´:
Python
"xlrd" package for extracting data from Excel files
---------------------------------------------------------------------------------
#coding=utf-8
import xlrd
import os, types, datetime
#excel´æ·ÅĿ¼
dir = u'D:\\temp\\excel'
......

pythonÖÐÈçºÎÅжÏÒ»¸ö±äÁ¿µÄÊý¾ÝÀàÐÍ£¿(Ô­´´)

import types
type(x) is types.IntType # ÅжÏÊÇ·ñint ÀàÐÍ
type(x) is types.StringType #ÊÇ·ñstringÀàÐÍ
.........
--------------------------------------------------------
³¬¼¶¶ñÐĵÄģʽ£¬²»ÓüÇסtypes.StringType
import types
type(x) == types(1) # ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ