Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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Ä£¿éÖ®codecs

python¶Ô¶à¹úÓïÑԵĴ¦ÀíÊÇÖ§³ÖµÄºÜºÃµÄ£¬Ëü¿ÉÒÔ´¦ÀíÏÖÔÚÈÎÒâ±àÂëµÄ×Ö·û£¬ÕâÀïÉîÈëµÄÑо¿Ò»ÏÂpython¶Ô¶àÖÖ²»Í¬ÓïÑԵĴ¦Àí¡£
    ÓÐÒ»µãÐèÒªÇå³þµÄÊÇ£¬µ±pythonÒª×ö±àÂëת»»µÄʱºò£¬»á½èÖúÓÚÄÚ²¿µÄ±àÂ룬ת»»¹ý³ÌÊÇÕâÑùµÄ£º
        Ô­ÓбàÂë -> ÄÚ²¿±àÂë ->
Ä¿ ......

¹ØÓÚ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 ´¦Àí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) # ......

Python ÖÐÎÄ×Ö·û´®µÄ½ØÈ¡

def
subString
(s,
length):
   
us = unicode(s, 'utf-8
')
    gs =
us.encode('gb2312
')
    n = int(length)
   
t = gs[:n]
    while True
:
        try
:
  &nb ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ