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
Python¡¢UnicodeºÍÖÐÎÄ[ת]
pythonµÄÖÐÎÄÎÊÌâÒ»Ö±ÊÇÀ§ÈÅÐÂÊÖµÄÍ·ÌÛÎÊÌ⣬ÕâÆªÎÄÕ½«¸øÄãÏêϸµØ½²½âÒ»ÏÂÕâ·½ÃæµÄ֪ʶ¡£µ±È»£¬¼¸ºõ¿ÉÒÔÈ·¶¨µÄÊÇ£¬ÔÚ½«À´µÄ°æ±¾ÖУ¬python»á³¹µ×½â¾ö´ËÎÊÌ⣬²»ÓÃÎÒÃÇÕâôÂé·³ÁË¡£
ÏÈÀ´¿´¿´pythonµÄ°æ±¾£º
>>> import sys
>> ......
>>> 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£¬±¾È˲»Ïë¶à˵ÁË£¬Èç¹û²»ÖªµÀʲôÊÇPython£¬¿ÉÒÔ¿´¡¶Ê²Ã´ÊÇPython
£¿¡·Ò»ÎÄ¡£Python¿ÉÒÔ¿ª·¢CGI³ÌÐò£¬ÄÇôÔÚApacheÏÂÓ¦ÈçºÎÅäÖÃÄØ£¿±¾ÎÄÖ»½²ÊöÁËÈçºÎ
ÒÔCGI·½Ê½ÅäÖÃApache£¬Ê¹ÆäÖ§³ÖPython³ÌÐò¡£ÆäËü·½Ê½£¬Èçmod_pythonÔò²»ÔÚÌÖÂÛ·¶Î§
(ÆäʵÊÇûÅä³É¹¦:-)¡£ÎÒËùʹÓõÄϵͳ»·¾³ÎªWindows 2000, Apache 1.3.19£ ......
Python MySQLdb ²éѯ·µ»Ø×Öµä½á¹¹ smallfish
MySQLdbĬÈϲéѯ½á¹û¶¼ÊÇ·µ»Øtuple£¬Êä³öʱºò²»ÊǺܷ½±ã£¬±ØÐë°´ÕÕ0£¬1ÕâÑù¶ÁÈ¡£¬ÎÞÒâÖÐÔÚÍøÉÏÕÒµ½¼òµ¥µÄÐ޸ķ½·¨£¬¾ÍÊÇ´«µÝÒ»¸öcursors.DictCursor¾ÍÐС£
ĬÈϳÌÐò£º
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', d ......
http://www.okpython.com/bbs/thread-3367-1-2.html
ÒÔ¶¯ÊÖʵ¼ùΪÈÙ , ÒÔÖ»¿´²»Á·Îª³Ü;
ÒÔ´òÓ¡ÈÕ־ΪÈÙ , ÒÔµ¥²½¸ú×ÙΪ³Ü;
ÒÔ¿Õ¸ñËõ½øÎªÈÙ , ÒÔÖÆ±íËõ½øÎª³Ü;
ÒÔµ¥Ôª²âÊÔΪÈÙ , ÒÔÈ˹¤²âÊÔΪ³Ü;
ÒÔÄ£¿é¸´ÓÃΪÈÙ , ÒÔ¸´ÖÆÕ³ÌùΪ³Ü;
ÒÔ¶à̬ӦÓÃΪÈÙ , ÒÔ·ÖÖ§ÅжÏΪ³Ü;
ÒÔPythonicΪÈÙ , ÒÔÈßÓàÍÏí³Îª³Ü;
ÒÔ×ܽá·ÖÏíΪ ......