锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲AV区无码字幕中文色,亚洲白嫩在线观看,亚洲av无码av制服另类专区http://www.tkk7.com/nkjava/category/47980.html緗韓嫻╃氱殑娌欐紶錛屾柟鍚戞渶涓洪噸瑕侊紝甯屾湜姝log鑳藉悜澶ф紶椹奸搩涓鏍鳳紝緇欐垜鏂瑰悜鍜屾寚寮曘? EJB/JPA/JSF/Struts/Spring/Hibernate/Perl/Shell/C/Java zh-cnSun, 14 Aug 2011 08:26:30 GMTSun, 14 Aug 2011 08:26:30 GMT60python 鍒ゆ柇 nullhttp://www.tkk7.com/nkjava/archive/2011/08/14/356482.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Sun, 14 Aug 2011 00:47:00 GMThttp://www.tkk7.com/nkjava/archive/2011/08/14/356482.htmlhttp://www.tkk7.com/nkjava/comments/356482.htmlhttp://www.tkk7.com/nkjava/archive/2011/08/14/356482.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/356482.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/356482.htmlif not result2 is  None :

if result is None

娉ㄦ剰鏄ぇ鍐?img src ="http://www.tkk7.com/nkjava/aggbug/356482.html" width = "1" height = "1" />

鑽夊師涓婄殑楠嗛┘ 2011-08-14 08:47 鍙戣〃璇勮
]]>
Python 榪炴帴 Mysqlhttp://www.tkk7.com/nkjava/archive/2011/08/14/356481.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Sun, 14 Aug 2011 00:46:00 GMThttp://www.tkk7.com/nkjava/archive/2011/08/14/356481.htmlhttp://www.tkk7.com/nkjava/comments/356481.htmlhttp://www.tkk7.com/nkjava/archive/2011/08/14/356481.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/356481.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/356481.html
#coding=utf-8

#MySQLdb 紺轟緥
#
##################################
import MySQLdb

#寤虹珛鍜屾暟鎹簱緋葷粺鐨勮繛鎺?br />conn 
= MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')

#鑾峰彇鎿嶄綔娓告爣
cursor 
= conn.cursor()
#鎵цSQL,鍒涘緩涓涓暟鎹簱.
cursor.execute(
"""create database python """)

#鍏抽棴榪炴帴錛岄噴鏀捐祫婧?br />cursor.close();
鎻掑叆鏁版嵁銆佹壒閲忔彃鍏ユ暟鎹?br />
#coding=utf-8
###################################
# @author migle
# @date 
2010-01-17
##################################
#MySQLdb 紺轟緥
#
##################################
import MySQLdb

#寤虹珛鍜屾暟鎹簱緋葷粺鐨勮繛鎺?br />conn 
= MySQLdb.connect(host='localhost', user='root',passwd='21ccvn')

#鑾峰彇鎿嶄綔娓告爣
cursor 
= conn.cursor()
#鎵цSQL,鍒涘緩涓涓暟鎹簱.
cursor.execute(
"""create database if not exists 21ccvn""")

#閫夋嫨鏁版嵁搴?br />conn.select_db(
'21ccvn');
#鎵цSQL,鍒涘緩涓涓暟鎹〃.
cursor.execute(
"""create table 21ccvn(id int, info varchar(100)) """)

value 
= [1,"inserted ?"];

#鎻掑叆涓鏉¤褰?br />cursor.execute(
"insert into test values(%s,%s)",value);

values
=[]


#鐢熸垚鎻掑叆鍙傛暟鍊?br />
for i in range(20):
    values.append((i,
'Hello mysqldb, I am recoder ' + str(i)))
#鎻掑叆澶氭潯璁板綍

cursor.executemany(
"""insert into test values(%s,%s) """,values);

#鍏抽棴榪炴帴錛岄噴鏀捐祫婧?br />cursor.close();
鏌ヨ錛岃幏鍙栦竴涓紝鑾峰彇澶氫釜錛岃幏鍙栨墍鏈夎褰曟暟
#coding=utf-8

#
# MySQLdb 鏌ヨ
#
#######################################

import MySQLdb

conn 
= MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='21ccvn')

cursor 
= conn.cursor()

count 
= cursor.execute('select * from test')

print 
'鎬誨叡鏈?nbsp;%s 鏉¤褰?/span>',count

#鑾峰彇涓鏉¤褰?姣忔潯璁板綍鍋氫負(fù)涓涓厓緇勮繑鍥?br />print 
"鍙幏鍙栦竴鏉¤褰?"
result 
= cursor.fetchone();
print result
#print 
'ID: %s   info: %s' % (result[0],result[1])
print 
'ID: %s   info: %s' % result 

#鑾峰彇5鏉¤褰曪紝娉ㄦ剰鐢變簬涔嬪墠鎵ц鏈変簡(jiǎn)fetchone()錛屾墍浠ユ父鏍囧凡緇忔寚鍒扮浜屾潯璁板綍浜?jiǎn)锛屼篃灏辨槸浠幗W簩鏉″紑濮嬬殑鎵鏈夎褰?br />print 
"鍙幏鍙?鏉¤褰?"
results 
= cursor.fetchmany(5)
for r in results:
    print r

print 
"鑾峰彇鎵鏈夌粨鏋?"
#閲嶇疆娓告爣浣嶇疆錛?/span>0,涓哄亸縐婚噺錛宮ode錛漚bsolute | relative,榛樿涓簉elative,
cursor.scroll(
0,mode='absolute')
#鑾峰彇鎵鏈夌粨鏋?br />results 
= cursor.fetchall()
for r in results:
    print r
conn.close()


鑽夊師涓婄殑楠嗛┘ 2011-08-14 08:46 鍙戣〃璇勮
]]>
python鐨則ime鍜宒ate澶勭悊http://www.tkk7.com/nkjava/archive/2011/03/04/345725.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:23:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345725.htmlhttp://www.tkk7.com/nkjava/comments/345725.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345725.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345725.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345725.html鍐呯疆妯″潡time鍖呭惈寰堝涓庢椂闂寸浉鍏沖嚱鏁般傛垜浠彲閫氳繃瀹冭幏寰楀綋鍓嶇殑鏃墮棿鍜屾牸寮忓寲鏃墮棿杈撳嚭銆?/p>

time()錛屼互嫻偣褰㈠紡榪斿洖鑷狶inux鏂頒笘綰互鏉ョ粡榪囩殑縐掓暟銆傚湪linux涓紝00:00:00 UTC, January 1, 1970鏄柊**49**鐨勫紑濮嬨?/p>
strftime鍙互鐢ㄦ潵鑾峰緱褰撳墠鏃墮棿錛屽彲浠ュ皢鏃墮棿鏍煎紡鍖栦負(fù)瀛楃涓茬瓑絳夛紝榪樻尯鏂逛究鐨勩備絾鏄渶瑕佹敞鎰忕殑鏄幏寰楃殑鏃墮棿鏄湇鍔″櫒鐨勬椂闂達(dá)紝娉ㄦ剰鏃跺尯闂錛屾瘮濡俫ae鎾掕皫閭d釜鐨勬椂闂村氨鏄牸鏋楀凹娌繪椂闂寸殑0鏃跺尯錛岄渶瑕佽嚜宸辮漿鎹€?br />
strftime錛堬級(jí)鍑芥暟灝嗘椂闂存牸寮忓寲
鎴戜滑鍙互浣跨敤strftime錛堬級(jí)鍑芥暟灝嗘椂闂存牸寮忓寲涓烘垜浠兂瑕佺殑鏍煎紡銆傚畠鐨勫師鍨嬪涓嬶細(xì)

size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);

鎴戜滑鍙互鏍規(guī)嵁format鎸囧悜瀛楃涓蹭腑鏍煎紡鍛戒護(hù)鎶妕imeptr涓繚瀛樼殑鏃墮棿淇℃伅鏀懼湪strDest鎸囧悜鐨勫瓧絎︿覆涓紝鏈澶氬悜strDest涓瓨鏀緈axsize涓瓧絎︺傝鍑芥暟榪斿洖鍚憇trDest鎸囧悜鐨勫瓧絎︿覆涓斁緗殑瀛楃鏁般?br />
strftime浣挎椂闂存牸寮忓寲銆俻ython鐨剆trftime鏍煎紡鏄疌搴撴敮鎸佺殑鏃墮棿鏍煎紡鐨勭湡瀛愰泦銆?br />
銆銆%a 鏄熸湡鍑犵殑綆鍐?Weekday name, abbr.
銆銆%A 鏄熸湡鍑犵殑鍏ㄧО Weekday name, full
銆銆%b 鏈堝垎鐨勭畝鍐?Month name, abbr.
銆銆%B 鏈堜喚鐨勫叏縐?Month name, full
銆銆%c 鏍囧噯鐨勬棩鏈熺殑鏃墮棿涓?Complete date and time representation
銆銆%d 鍗佽繘鍒惰〃紺虹殑姣忔湀鐨勭鍑犲ぉ Day of the month
銆銆%H 24灝忔椂鍒剁殑灝忔椂 Hour (24-hour clock)
銆銆%I 12灝忔椂鍒剁殑灝忔椂 Hour (12-hour clock)
銆銆%j 鍗佽繘鍒惰〃紺虹殑姣忓勾鐨勭鍑犲ぉ Day of the year
銆銆%m 鍗佽繘鍒惰〃紺虹殑鏈堜喚 Month number
銆銆%M 鍗佹椂鍒惰〃紺虹殑鍒嗛挓鏁?Minute number
銆銆%S 鍗佽繘鍒剁殑縐掓暟 Second number
銆銆%U 絎勾鐨勭鍑犲懆錛屾妸鏄熸湡鏃ュ仛涓虹涓澶╋紙鍊間粠0鍒?3錛塛eek number (Sunday first weekday)
銆銆%w 鍗佽繘鍒惰〃紺虹殑鏄熸湡鍑狅紙鍊間粠0鍒?錛屾槦鏈熷ぉ涓?錛墂eekday number
銆銆%W 姣忓勾鐨勭鍑犲懆錛屾妸鏄熸湡涓鍋氫負(fù)絎竴澶╋紙鍊間粠0鍒?3錛?Week number (Monday first weekday)
銆銆%x 鏍囧噯鐨勬棩鏈熶覆 Complete date representation (e.g. 13/01/08)
銆銆%X 鏍囧噯鐨勬椂闂翠覆 Complete time representation (e.g. 17:02:10)
銆銆%y 涓嶅甫涓栫邯鐨勫崄榪涘埗騫翠喚錛堝間粠0鍒?9錛塝ear number within century
銆銆%Y 甯︿笘綰儴鍒嗙殑鍗佸埗騫翠喚 Year number
銆銆%z錛?Z 鏃跺尯鍚嶇О錛屽鏋滀笉鑳藉緱鍒版椂鍖哄悕縐板垯榪斿洖絀哄瓧絎︺侼ame of time zone
銆銆%% 鐧懼垎鍙?

1. # handling date/time data
   2. # Python23 tested vegaseat 3/6/2005
   3.
   4. import time
   5.
   6. print "List the functions within module time:"
   7. for funk in dir(time):
   8. print funk
   9.
  10. print time.time(), "seconds since 1/1/1970 00:00:00"
  11. print time.time()/(60*60*24), "days since 1/1/1970"
  12.
  13. # time.clock() gives wallclock seconds, accuracy better than 1 ms
  14. # time.clock() is for windows, time.time() is more portable
  15. print "Using time.clock() = ", time.clock(), "seconds since first call to clock()"
  16. print "\nTiming a 1 million loop 'for loop' ..."
  17. start = time.clock()
  18. for x in range(1000000):
  19. y = x # do something
  20. end = time.clock()
  21. print "Time elapsed = ", end - start, "seconds"
  22.
  23. # create a tuple of local time data
  24. timeHere = time.localtime()
  25. print "\nA tuple of local date/time data using time.localtime():"
  26. print "(year,month,day,hour,min,sec,weekday(Monday=0),yearday,dls-flag)"
  27. print timeHere
  28.
  29. # extract a more readable date/time from the tuple
  30. # eg. Sat Mar 05 22:51:55 2005
  31. print "\nUsing time.asctime(time.localtime()):", time.asctime(time.localtime())
  32. # the same results
  33. print "\nUsing time.ctime(time.time()):", time.ctime(time.time())
  34. print "\nOr using time.ctime():", time.ctime()
  35.
  36. print "\nUsing strftime():"
  37. print "Day and Date:", time.strftime("%a %m/%d/%y", time.localtime())
  38. print "Day, Date :", time.strftime("%A, %B %d, %Y", time.localtime())
  39. print "Time (12hr) :", time.strftime("%I:%M:%S %p", time.localtime())
  40. print "Time (24hr) :", time.strftime("%H:%M:%S", time.localtime())
  41. print "DayMonthYear:",time.strftime("%d%b%Y", time.localtime())
  42.
  43. print
  44.
  45. print "Start a line with this date-time stamp and it will sort:",\
  46. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())
  47.
  48. print
  49.
  50. def getDayOfWeek(dateString):
  51. # day of week (Monday = 0) of a given month/day/year
  52. t1 = time.strptime(dateString,"%m/%d/%Y")
  53. # year in time_struct t1 can not go below 1970 (start of epoch)!
  54. t2 = time.mktime(t1)
  55. return(time.localtime(t2)[6])
  56.
  57. Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
  58. 'Friday', 'Saturday', 'Sunday']
  59.
  60. # sorry about the limitations, stay above 01/01/1970
  61. # more exactly 01/01/1970 at 0 UT (midnight Greenwich, England)
  62. print "11/12/1970 was a", Weekday[getDayOfWeek("11/12/1970")]
  63.
  64. print
  65.
  66. print "Calculate difference between two times (12 hour format) of a day:"
  67. time1 = raw_input("Enter first time (format 11:25:00AM or 03:15:30PM): ")
  68. # pick some plausible date
  69. timeString1 = "03/06/05 " + time1
  70. # create a time tuple from this time string format eg. 03/06/05 11:22:00AM
  71. timeTuple1 = time.strptime(timeString1, "%m/%d/%y %I:%M:%S%p")
  72.
  73. #print timeTuple1 # test eg. (2005, 3, 6, 11, 22, 0, 5, 91, -1)
  74.
  75. time2 = raw_input("Enter second time (format 11:25:00AM or 03:15:30PM): ")
  76. # use same date to stay in same day
  77. timeString2 = "03/06/05 " + time2
  78. timeTuple2 = time.strptime(timeString2, "%m/%d/%y %I:%M:%S%p")
  79.
  80. # mktime() gives seconds since epoch 1/1/1970 00:00:00
  81. time_difference = time.mktime(timeTuple2) - time.mktime(timeTuple1)
  82. #print type(time_difference) # test <type 'float'>
  83. print "Time difference = %d seconds" % int(time_difference)
  84. print "Time difference = %0.1f minutes" % (time_difference/60.0)
  85. print "Time difference = %0.2f hours" % (time_difference/(60.0*60))
  86.
  87. print
  88.
  89. print "Wait one and a half seconds!"
  90. time.sleep(1.5)
  91. print "The end!"



鑽夊師涓婄殑楠嗛┘ 2011-03-04 16:23 鍙戣〃璇勮
]]>
Python IOhttp://www.tkk7.com/nkjava/archive/2011/03/04/345724.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:10:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345724.htmlhttp://www.tkk7.com/nkjava/comments/345724.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345724.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345724.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345724.html

1.open

浣跨敤open鎵撳紑鏂囦歡鍚庝竴瀹氳璁板緱璋冪敤鏂囦歡瀵硅薄鐨刢lose()鏂規(guī)硶銆傛瘮濡傚彲浠ョ敤try/finally璇彞鏉ョ‘淇濇渶鍚庤兘鍏抽棴鏂囦歡銆?/p>

file_object = open('thefile.txt')
try:
     all_the_text 
= file_object.read( )
finally:
     file_object.close( )


娉細(xì)涓嶈兘鎶妎pen璇彞鏀懼湪try鍧楅噷錛屽洜涓哄綋鎵撳紑鏂囦歡鍑虹幇寮傚父鏃訛紝鏂囦歡瀵硅薄file_object鏃犳硶鎵цclose()鏂規(guī)硶銆?/p>

2.璇繪枃浠?/span>

璇繪枃鏈枃浠?/h4>
input = open('data''r')
#絎簩涓弬鏁伴粯璁や負(fù)r
input = open('data')

 

璇諱簩榪涘埗鏂囦歡

input = open('data''rb')

 

璇誨彇鎵鏈夊唴瀹?/h4>
file_object = open('thefile.txt')
try:
     all_the_text 
= file_object.read( )
finally:
     file_object.close( )

 

璇誨浐瀹氬瓧鑺?/h4>
file_object = open('abinfile''rb')
try:
    
while True:
         chunk 
= file_object.read(100)
        
if not chunk:
            
break
         do_something_with(chunk)
finally:
     file_object.close( )

 

璇繪瘡琛?/h4>
list_of_all_the_lines = file_object.readlines( )


濡傛灉鏂囦歡鏄枃鏈枃浠訛紝榪樺彲浠ョ洿鎺ラ亶鍘嗘枃浠跺璞¤幏鍙栨瘡琛岋細(xì)

for line in file_object:
     process line

 

3.鍐欐枃浠?/span>

鍐欐枃鏈枃浠?/h4>
output = open('data''w')

 

鍐欎簩榪涘埗鏂囦歡

output = open('data''wb')

 

榪藉姞鍐欐枃浠?/h4>
output = open('data''w+')

 

鍐欐暟鎹?/h4>
file_object = open('thefile.txt''w')
file_object.write(all_the_text)
file_object.close( )

 

鍐欏叆澶氳

file_object.writelines(list_of_text_strings)


娉ㄦ剰錛岃皟鐢╳ritelines鍐欏叆澶氳鍦ㄦц兘涓婁細(xì)姣斾嬌鐢╳rite涓嬈℃у啓鍏ヨ楂樸?/p>

鍦ㄥ鐞嗘棩蹇楁枃浠剁殑鏃跺欙紝甯稿父浼?xì)閬囧埌杩欐狅L(fēng)殑鎯呭喌錛氭棩蹇楁枃浠跺法澶э紝涓嶅彲鑳戒竴嬈℃ф妸鏁翠釜鏂囦歡璇誨叆鍒板唴瀛樹腑榪涜澶勭悊錛屼緥濡傞渶瑕佸湪涓鍙扮墿鐞嗗唴瀛樹負(fù) 2GB 鐨勬満鍣ㄤ笂澶勭悊涓涓?2GB 鐨勬棩蹇楁枃浠訛紝鎴戜滑鍙兘甯屾湜姣忔鍙鐞嗗叾涓?200MB 鐨勫唴瀹廣?br style="line-height: normal" /> 鍦?Python 涓紝鍐呯疆鐨?File 瀵硅薄鐩存帴鎻愪緵浜?jiǎn)涓涓?readlines(sizehint) 鍑芥暟鏉ュ畬鎴愯繖鏍風(fēng)殑浜嬫儏銆備互涓嬮潰鐨勪唬鐮佷負(fù)渚嬶細(xì)

file = open('test.log', 'r')
sizehint
= 209715200   # 200M
position = 0
lines
= file.readlines(sizehint)
while not file.tell() - position < 0:
     position
= file.tell()
     lines
= file.readlines(sizehint)



姣忔璋冪敤 readlines(sizehint) 鍑芥暟錛屼細(xì)榪斿洖澶х害 200MB 鐨勬暟鎹紝鑰屼笖鎵榪斿洖鐨勫繀鐒墮兘鏄畬鏁寸殑琛屾暟鎹紝澶у鏁版儏鍐典笅錛岃繑鍥炵殑鏁版嵁鐨勫瓧鑺傛暟浼?xì)绋嶅井姣?sizehint 鎸囧畾鐨勫煎ぇ涓鐐癸紙闄ゆ渶鍚庝竴嬈¤皟鐢?readlines(sizehint) 鍑芥暟鐨勬椂鍊欙級(jí)銆傞氬父鎯呭喌涓嬶紝Python 浼?xì)鑷姩灏嗙敤鎴锋寚瀹氱?sizehint 鐨勫艱皟鏁存垚鍐呴儴緙撳瓨澶у皬鐨勬暣鏁板嶃?/div>

file鍦╬ython鏄竴涓壒孌婄殑綾誨瀷錛屽畠鐢ㄤ簬鍦╬ython紼嬪簭涓澶栭儴鐨勬枃浠惰繘琛屾搷浣溿傚湪python涓竴鍒囬兘鏄璞★紝file涔熶笉渚嬪錛宖ile鏈塮ile鐨勬柟娉曞拰灞炴с備笅闈㈠厛鏉ョ湅濡備綍鍒涘緩涓涓猣ile瀵硅薄錛?/div>

file(name[, mode[, buffering]])
file()鍑芥暟鐢ㄤ簬鍒涘緩涓涓猣ile瀵硅薄錛屽畠鏈変竴涓埆鍚嶅彨open()錛屽彲鑳芥洿褰㈣薄涓浜涳紝瀹冧滑鏄唴緗嚱鏁般傛潵鐪嬬湅瀹冪殑鍙傛暟銆傚畠鍙傛暟閮芥槸浠ュ瓧絎︿覆鐨勫艦寮忎紶閫掔殑銆俷ame鏄枃浠剁殑鍚嶅瓧銆?/div>
mode鏄墦寮鐨勬ā寮忥紝鍙夌殑鍊間負(fù)r w a U錛屽垎鍒唬琛ㄨ錛堥粯璁わ級(jí) 鍐?娣誨姞 鏀寔鍚勭鎹㈣絎︾殑妯″紡銆傜敤w鎴朼妯″紡鎵撳紑鏂囦歡鐨勮瘽錛屽鏋滄枃浠朵笉瀛樺湪錛岄偅涔堝氨鑷姩鍒涘緩銆傛澶栵紝鐢╳妯″紡鎵撳紑涓涓凡緇忓瓨鍦ㄧ殑鏂囦歡鏃訛紝鍘熸湁鏂囦歡鐨勫唴瀹逛細(xì)琚竻絀猴紝鍥犱負(fù)涓寮濮嬫枃浠剁殑鎿嶄綔鐨勬爣璁版槸鍦ㄦ枃浠剁殑寮澶寸殑錛岃繖鏃跺欒繘琛屽啓鎿嶄綔錛屾棤鐤戜細(xì)鎶婂師鏈夌殑鍐呭緇欐姽鎺夈傜敱浜庡巻鍙茬殑鍘熷洜錛屾崲琛岀鍦ㄤ笉鍚岀殑緋葷粺涓湁涓嶅悓妯″紡錛屾瘮濡傚湪 unix涓槸涓涓猏n錛岃屽湪windows涓槸‘\r\n’錛岀敤U妯″紡鎵撳紑鏂囦歡錛屽氨鏄敮鎸佹墍鏈夌殑鎹㈣妯″紡錛屼篃灝辮‘\r’ '\n' '\r\n'閮藉彲琛ㄧず鎹㈣錛屼細(xì)鏈変竴涓猼uple鐢ㄦ潵瀛樿串榪欎釜鏂囦歡涓敤鍒拌繃鐨勬崲琛岀銆備笉榪囷紝铏借鎹㈣鏈夊縐嶆ā寮忥紝璇誨埌python涓粺涓鐢╘n浠f浛銆傚湪妯″紡瀛楃鐨勫悗闈紝榪樺彲浠ュ姞涓? b t榪欎袱縐嶆爣璇嗭紝鍒嗗埆琛ㄧず鍙互瀵規(guī)枃浠跺悓鏃惰繘琛岃鍐欐搷浣滃拰鐢ㄤ簩榪涘埗妯″紡銆佹枃鏈ā寮忥紙榛樿錛夋墦寮鏂囦歡銆?/div>
buffering濡傛灉涓?琛ㄧず涓嶈繘琛岀紦鍐?濡傛灉涓?琛ㄧず榪涜“琛岀紦鍐?#8220;;濡傛灉鏄竴涓ぇ浜?鐨勬暟琛ㄧず緙撳啿鍖虹殑澶у皬錛屽簲璇ユ槸浠ュ瓧鑺備負(fù)鍗曚綅鐨勩?/div>

file瀵硅薄鏈夎嚜宸辯殑灞炴у拰鏂規(guī)硶銆傚厛鏉ョ湅鐪媐ile鐨勫睘鎬с?/div>

closed #鏍囪鏂囦歡鏄惁宸茬粡鍏抽棴錛岀敱close()鏀瑰啓
encoding #鏂囦歡緙栫爜
mode #鎵撳紑妯″紡
name #鏂囦歡鍚?/div>
newlines #鏂囦歡涓敤鍒扮殑鎹㈣妯″紡錛屾槸涓涓猼uple
softspace #boolean鍨嬶紝涓鑸負(fù)0錛屾嵁璇寸敤浜巔rint

file鐨勮鍐欐柟娉曪細(xì)

F.read([size]) #size涓鴻鍙栫殑闀垮害錛屼互byte涓哄崟浣?/div>
F.readline([size]) 
#璇諱竴琛岋紝濡傛灉瀹氫箟浜?jiǎn)size錛屾湁鍙兘榪斿洖鐨勫彧鏄竴琛岀殑涓閮ㄥ垎
F.readlines([size]) 
#鎶婃枃浠舵瘡涓琛屼綔涓轟竴涓猯ist鐨勪竴涓垚鍛橈紝騫惰繑鍥炶繖涓猯ist銆傚叾瀹炲畠鐨勫唴閮ㄦ槸閫氳繃寰幆璋冪敤readline()鏉ュ疄鐜扮殑銆傚鏋滄彁渚泂ize鍙傛暟錛宻ize鏄〃紺鴻鍙栧唴瀹圭殑鎬婚暱錛屼篃灝辨槸璇村彲鑳藉彧璇誨埌鏂囦歡鐨勪竴閮ㄥ垎銆?/div>
F.write(str) 
#鎶妔tr鍐欏埌鏂囦歡涓紝write()騫朵笉浼?xì)鍦╯tr鍚庡姞涓婁竴涓崲琛岀
F.writelines(seq) 
#鎶妔eq鐨勫唴瀹瑰叏閮ㄥ啓鍒版枃浠朵腑銆傝繖涓嚱鏁頒篃鍙槸蹇犲疄鍦板啓鍏ワ紝涓嶄細(xì)鍦ㄦ瘡琛屽悗闈㈠姞涓婁換浣曚笢瑗褲?/div>
file鐨勫叾浠栨柟娉曪細(xì)

F.close() 
#鍏抽棴鏂囦歡銆俻ython浼?xì)鍦ㄤ竴涓枃浠朵笉鐢ㄥ悗鑷姩鍏抽棴鏂囦歡錛屼笉榪囪繖涓鍔熻兘娌℃湁淇濊瘉錛屾渶濂借繕鏄吇鎴愯嚜宸卞叧闂殑涔?fàn)鎯傚鏋滀竴涓枃浠跺湪鍏抽棴鍚庤繕瀵瑰叾榪涜鎿嶄綔浼?xì)漶旂敓ValueError
F.flush() 
#鎶婄紦鍐插尯鐨勫唴瀹瑰啓鍏ョ‖鐩?/div>
F.fileno() 
#榪斿洖涓涓暱鏁村瀷鐨?#8221;鏂囦歡鏍囩“
F.isatty() 
#鏂囦歡鏄惁鏄竴涓粓绔澶囨枃浠訛紙unix緋葷粺涓殑錛?/div>
F.tell() 
#榪斿洖鏂囦歡鎿嶄綔鏍囪鐨勫綋鍓嶄綅緗紝浠ユ枃浠剁殑寮澶翠負(fù)鍘熺偣
F.next() 
#榪斿洖涓嬩竴琛岋紝騫跺皢鏂囦歡鎿嶄綔鏍囪浣嶇Щ鍒頒笅涓琛屻傛妸涓涓猣ile鐢ㄤ簬for ... in file榪欐牱鐨勮鍙ユ椂錛屽氨鏄皟鐢╪ext()鍑芥暟鏉ュ疄鐜伴亶鍘嗙殑銆?/div>
F.seek(offset[,whence]) 
#灝嗘枃浠舵墦鎿嶄綔鏍囪縐誨埌offset鐨勪綅緗傝繖涓猳ffset涓鑸槸鐩稿浜庢枃浠剁殑寮澶存潵璁$畻鐨勶紝涓鑸負(fù)姝f暟銆備絾濡傛灉鎻愪緵浜?jiǎn)whence鍙傛暟灝變笉涓瀹氫簡(jiǎn)錛寃hence鍙互涓?琛ㄧず浠庡ご寮濮嬭綆楋紝1琛ㄧず浠ュ綋鍓嶄綅緗負(fù)鍘熺偣璁$畻銆?琛ㄧず浠ユ枃浠舵湯灝句負(fù)鍘熺偣榪涜璁$畻銆傞渶瑕佹敞鎰忥紝濡傛灉鏂囦歡浠鎴朼+鐨勬ā寮忔墦寮錛屾瘡嬈¤繘琛屽啓鎿嶄綔鏃訛紝鏂囦歡鎿嶄綔鏍囪浼?xì)鑷姩杩斿洖鍒版枃錃g鏈熬銆?/div>
F.truncate([size]) 
#鎶婃枃浠惰鎴愯瀹氱殑澶у皬錛岄粯璁ょ殑鏄鍒板綋鍓嶆枃浠舵搷浣滄爣璁扮殑浣嶇疆銆傚鏋渟ize姣旀枃浠剁殑澶у皬榪樿澶э紝渚濇嵁緋葷粺鐨勪笉鍚屽彲鑳芥槸涓嶆敼鍙樻枃浠訛紝涔熷彲鑳芥槸鐢?鎶婃枃浠惰ˉ鍒扮浉搴旂殑澶у皬錛屼篃鍙兘鏄互涓浜涢殢鏈虹殑鍐呭鍔犱笂鍘匯?/div>

 



]]>Python Mysql http://www.tkk7.com/nkjava/archive/2011/03/04/345723.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:09:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345723.htmlhttp://www.tkk7.com/nkjava/comments/345723.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345723.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345723.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345723.html 

http://www.kitebird.com/articles/pydbapi.html

http://www.codegood.com/  python mysql椹卞姩涓嬭澆澶у叏


the Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard.

You can choose the right database for your application. Python Database API supports a wide range of database servers:

  • GadFly
  • mSQL
  • MySQL
  • PostgreSQL
  • Microsoft SQL Server 2000
  • Informix
  • Interbase
  • Oracle
  • Sybase

Here is the list of available Python databases interfaces:

Python Database Interfaces and APIs
            

You must download a separate DB API module for each database you need to access. For example, if you need to access an Oracle database as well as a MySQL database, you must download both the Oracle and the MySQL database modules.

The DB API provides a minimal standard for working with databases, using Python structures and syntax wherever possible. This API includes the following:

  • Importing the api module.

  • Acquiring a connection with the database.

  • Issuing SQL statements and stored procedures.

  • Closing the connection

We would learn all the concepts using MySQL so let's talk about MySQLdb module only.

What is MySQLdb?

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0, and is built on top of the MySQL C API.

How do I install the MySQLdb?

Before proceeding you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it:

#!/usr/bin/python
            import MySQLdb
            

If it produces following result then it means MySQLdb module is not installed:

Traceback (most recent call last):
            File "test.py", line 3, in <module>
            import MySQLdb
            ImportError: No module named MySQLdb
            

To install MySQLdb module, download it from MySQLdb Download page and proceed as follows:

$ gunzip MySQL-python-1.2.2.tar.gz
            $ tar -xvf MySQL-python-1.2.2.tar
            $ cd MySQL-python-1.2.2
            $ python setup.py build
            $ python setup.py install
            

Note: Make sure you have root privilege to install above module.

Database Connection:

Before connecting to a MySQL database make sure followings:

  • You have created a database TESTDB.

  • You have created a table EMPLOYEE in TESTDB.

  • This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.

  • User ID "testuser" and password "test123" are set to access TESTDB

  • Python module MySQLdb is installed properly on your machine.

  • You have gone through MySQL tutorial to understand MySQL Basics.

Example:

Following is the example of connecting with MySQL database "TESTDB"

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # execute SQL query using execute() method.
            cursor.execute("SELECT VERSION()")
            # Fetch a single row using fetchone() method.
            data = cursor.fetchone()
            print "Database version : %s " % data
            # disconnect from server
            db.close()
            

While running this script, its producing following result at my Linux machine.

Database version : 5.0.45
            

If a connection is established with the datasource then a Connection Object is returned and saved into db for further use otherwise db is set to None. Next db object is used to create a cursor object which in turn is used to execute SQL queries.

Finally before coming out it ensures that database connection is closed and resources are released.

Creating Database Table:

Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor.

Example:

First let's create Database table EMPLOYEE:

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Drop table if it already exist using execute() method.
            cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
            # Create table as per requirement
            sql = """CREATE TABLE EMPLOYEE (
            FIRST_NAME  CHAR(20) NOT NULL,
            LAST_NAME  CHAR(20),
            AGE INT,
            SEX CHAR(1),
            INCOME FLOAT )"""
            cursor.execute(sql)
            # disconnect from server
            db.close()
            

INSERT Operation:

INSERT operation is required when you want to create your records into a database table.

Example:

Following is the example which executes SQL INSERT statement to create a record into EMPLOYEE table.

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Prepare SQL query to INSERT a record into the database.
            sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
            LAST_NAME, AGE, SEX, INCOME)
            VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
            except:
            # Rollback in case there is any error
            db.rollback()
            # disconnect from server
            db.close()
            

Above example can be written as follows to create SQL queries dynamically:

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Prepare SQL query to INSERT a record into the database.
            sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
            LAST_NAME, AGE, SEX, INCOME) \
            VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
            ('Mac', 'Mohan', 20, 'M', 2000)
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
            except:
            # Rollback in case there is any error
            db.rollback()
            # disconnect from server
            db.close()
            

Example:

Following code segment is another form of execute where you can pass parameters directly:

..................................
            user_id = "test123"
            password = "password"
            con.execute('insert into Login values("%s", "%s")' % \
            (user_id, password))
            ..................................
            

READ Operation:

READ Operation on any databasse means to fetch some useful information from the database.

Once our database connection is established, we are ready to make a query into this database. We can use either fetchone() method to fetch single record or fetchall method to fetech multiple values from a database table.

  • fetchone(): This method fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.

  • fetchall(): This method fetches all the rows in a result set. If some rows have already been extracted from the result set, the fetchall() method retrieves the remaining rows from the result set.

  • rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute() method.

Example:

Following is the procedure to query all the records from EMPLOYEE table having salary more than 1000.

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Prepare SQL query to INSERT a record into the database.
            sql = "SELECT * FROM EMPLOYEE \
            WHERE INCOME > '%d'" % (1000)
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Fetch all the rows in a list of lists.
            results = cursor.fetchall()
            for row in results:
            fname = row[0]
            lname = row[1]
            age = row[2]
            sex = row[3]
            income = row[4]
            # Now print fetched result
            print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
            (fname, lname, age, sex, income )
            except:
            print "Error: unable to fecth data"
            # disconnect from server
            db.close()
            

This will produce following result:

fname=Mac, lname=Mohan, age=20, sex=M, income=2000
            

Update Operation:

UPDATE Operation on any databasse means to update one or more records which are already available in the database. Following is the procedure to update all the records having SEX as 'M'. Here we will increase AGE of all the males by one year.

Example:

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Prepare SQL query to UPDATE required records
            sql = "UPDATE EMPLOYEE SET AGE = AGE + 1
            WHERE SEX = '%c'" % ('M')
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
            except:
            # Rollback in case there is any error
            db.rollback()
            # disconnect from server
            db.close()
            

DELETE Operation:

DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from EMPLOYEE where AGE is more than 20.

Example:

#!/usr/bin/python
            import MySQLdb
            # Open database connection
            db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
            # prepare a cursor object using cursor() method
            cursor = db.cursor()
            # Prepare SQL query to DELETE required records
            sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
            except:
            # Rollback in case there is any error
            db.rollback()
            # disconnect from server
            db.close()
            

Performing Transactions:

Transactions are a mechanism that ensures data consistency. Transactions should have the following four properties:

  • Atomicity: Either a transaction completes or nothing happens at all.

  • Consistency: A transaction must start in a consistent state and leave the system is a consistent state.

  • Isolation: Intermediate results of a transaction are not visible outside the current transaction.

  • Durability: Once a transaction was committed, the effects are persistent, even after a system failure.

The Python DB API 2.0 provides two methods to either commit or rollback a transaction.

Example:

You already have seen how we have implemented transations. Here is again similar example:

# Prepare SQL query to DELETE required records
            sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
            try:
            # Execute the SQL command
            cursor.execute(sql)
            # Commit your changes in the database
            db.commit()
            except:
            # Rollback in case there is any error
            db.rollback()
            

COMMIT Operation:

Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted back.

Here is a simple example to call commit method.

    db.commit()
            

ROLLBACK Operation:

If you are not satisfied with one or more of the changes and you want to revert back those changes completely then use rollback method.

Here is a simple example to call rollback metho.

   db.rollback()
            

Disconnecting Database:

To disconnect Database connection, use close() method.

    db.close()
            

If the connection to a database is closed by the user with the close() method, any outstanding transactions are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly.

Handling Errors:

There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already canceled or finished statement handle.

The DB API defines a number of errors that must exist in each database module. The following table lists these exceptions.

Exception Description
Warning Used for non-fatal issues. Must subclass StandardError.
Error Base class for errors. Must subclass StandardError.
InterfaceError Used for errors in the database module, not the database itself. Must subclass Error.
DatabaseError Used for errors in the database. Must subclass Error.
DataError Subclass of DatabaseError that refers to errors in the data.
OperationalError Subclass of DatabaseError that refers to errors such as the loss of a connection to the database. These errors are generally outside of the control of the Python scripter.
IntegrityError Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys.
InternalError Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active.
ProgrammingError Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you.
NotSupportedError Subclass of DatabaseError that refers to trying to call unsupported functionality.

Your Python scripts should handle these errors but before using any of the above exceptions, make sure your MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0 specification.



]]>
python 涓枃涔辯爜http://www.tkk7.com/nkjava/archive/2011/03/04/345722.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:07:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345722.htmlhttp://www.tkk7.com/nkjava/comments/345722.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345722.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345722.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345722.html

]]>
Python 姝e垯琛ㄨ揪寮?/title><link>http://www.tkk7.com/nkjava/archive/2011/03/04/345721.html</link><dc:creator>鑽夊師涓婄殑楠嗛┘</dc:creator><author>鑽夊師涓婄殑楠嗛┘</author><pubDate>Fri, 04 Mar 2011 08:06:00 GMT</pubDate><guid>http://www.tkk7.com/nkjava/archive/2011/03/04/345721.html</guid><wfw:comment>http://www.tkk7.com/nkjava/comments/345721.html</wfw:comment><comments>http://www.tkk7.com/nkjava/archive/2011/03/04/345721.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/nkjava/comments/commentRss/345721.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/nkjava/services/trackbacks/345721.html</trackback:ping><description><![CDATA[     鎽樿: http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 姝e垯琛ㄨ揪寮忓熀紜 1.1. 綆鍗曚粙緇?姝e垯琛ㄨ揪寮忓茍涓嶆槸Python鐨勪竴閮ㄥ垎銆傛鍒欒〃杈懼紡鏄敤浜庡鐞嗗瓧絎︿覆鐨勫己澶у伐鍏鳳紝鎷ユ湁鑷繁鐙壒鐨勮娉曚互鍙?qiáng)涓涓嫭绔嬬殑澶勭悊寮曟搸錛屾晥鐜囦笂鍙兘涓嶅str鑷甫鐨勬柟娉曪紝浣嗗姛鑳藉崄鍒嗗己澶с傚緱鐩婁簬榪欎竴鐐癸紝鍦ㄦ彁渚涗簡(jiǎn)姝e垯琛ㄨ揪寮忕殑璇█閲岋紝姝e垯琛ㄨ揪寮忕殑璇硶閮芥槸涓鏍?..  <a href='http://www.tkk7.com/nkjava/archive/2011/03/04/345721.html'>闃呰鍏ㄦ枃</a><img src ="http://www.tkk7.com/nkjava/aggbug/345721.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/nkjava/" target="_blank">鑽夊師涓婄殑楠嗛┘</a> 2011-03-04 16:06 <a href="http://www.tkk7.com/nkjava/archive/2011/03/04/345721.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Python Beautiful Souphttp://www.tkk7.com/nkjava/archive/2011/03/04/345720.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:05:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345720.htmlhttp://www.tkk7.com/nkjava/comments/345720.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345720.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345720.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345720.htmlBeautiful Soup 涓枃鏂囨。 http://www.crummy.com/software/BeautifulSoup/documentation.zh.html#Using%20tag%20names%20as%20members

http://www.meegoq.com/forum.php?mod=viewthread&tid=63&extra=

http://edu.codepub.com/2010/0208/20340.php

http://www.crummy.com/software/BeautifulSoup/documentation.zh.html

]]>
Python UUIDhttp://www.tkk7.com/nkjava/archive/2011/03/04/345718.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:03:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345718.htmlhttp://www.tkk7.com/nkjava/comments/345718.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345718.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345718.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345718.htmlUUID objects (universally unique identifiers) according to RFC 4122.
UUID objects (universally unique identifiers) according to RFC 
4122.
 
This module provides immutable UUID objects (
class UUID) and the functions
uuid1(), uuid3(), uuid4(), uuid5() 
for generating version 134and 5
UUIDs as specified 
in RFC 4122.
 
If all you want 
is a unique ID, you should probably call uuid1() or uuid4().
Note that uuid1() may compromise privacy since it creates a UUID containing
the computer
's network address.  uuid4() creates a random UUID.
 
Typical usage:
 
    
>>> import uuid
 
    
# make a UUID based on the host ID and current time
    >>> uuid.uuid1()
    UUID(
'a8098c1a-f86e-11da-bd1a-00112444be1e')
 
    
# make a UUID using an MD5 hash of a namespace UUID and a name
    >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
    UUID(
'6fa459ea-ee8a-3ca4-894e-db77e160355e')
 
    
# make a random UUID
    >>> uuid.uuid4()
    UUID(
'16fd2706-8baf-433b-82eb-8c7fada847da')
 
    
# make a UUID using a SHA-1 hash of a namespace UUID and a name
    >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
    UUID(
'886313e1-3b8a-5372-9b90-0c9aee199e5d')
 
    
# make a UUID from a string of hex digits (braces and hyphens ignored)
    >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
 
    
# convert a UUID to a string of hex digits in standard form
    >>> str(x)
    
'00010203-0405-0607-0809-0a0b0c0d0e0f'
 
    
# get the raw 16 bytes of the UUID
    >>> x.bytes
    
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t \x0b\x0c \x0e\x0f'
 
    
# make a UUID from a 16-byte string
    >>> uuid.UUID(bytes=x.bytes)
    UUID(
'00010203-0405-0607-0809-0a0b0c0d0e0f')
 
This module works with Python 
2.3 or higher.


]]>Python Urllib2http://www.tkk7.com/nkjava/archive/2011/03/04/345717.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 08:02:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345717.htmlhttp://www.tkk7.com/nkjava/comments/345717.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345717.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345717.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345717.html
http://docs.python.org/library/urllib.html


璁劇疆瓚呮椂鏃墮棿

     import socket

import urllib2

socket.setdefaulttimeout(seconds)

open = urllib2.urlopen("

榪噑ocket.setdefaulttimeout() 璁劇疆浜?jiǎn)鍏ㄥ眬榛樿瓚呮椂鏃墮棿錛屼粠鑰岀粰urllibe2.urlopen()涔熻緗簡(jiǎn)榛樿鐨勮秴鏃舵椂闂?br />




鎶撳彇鍥劇墖(榪欎釜渚嬪瓙閲岀殑Request鎵句笉鍒幫紝鎴戜竴鑸彧鏈塽rllib2)
soup=urlllib2.open(url)

# Let's create a function that downloads a file, and saves it locally.
#
 This function accepts a file name, a read/write mode(binary or text),
#
 and the base url.

def stealStuff(file_name,file_mode,base_url):
    
from urllib2 import Request, urlopen, URLError, HTTPError

    
#create the url and the request
    url = base_url + file_name
    req 
= Request(url)

    
# Open the url
    try:
        f 
= urlopen(req)
        
print "downloading " + url

        
# Open our local file for writing
        local_file = open(file_name, "w" + file_mode)
        
#Write to our local file
        local_file.write(f.read())
        local_file.close()

    
#handle errors
    except HTTPError, e:
        
print "HTTP Error:",e.code , url
    
except URLError, e:
        
print "URL Error:",e.reason , url

# Set the range of images to 1-50.It says 51 because the
#
 range function never gets to the endpoint.
image_range = range(1,51)

# Iterate over image range
for index in image_range:

    base_url 
= 'http://www.techniqal.com/'
    
#create file name based on known pattern
    file_name =  str(index) + ".jpg"
    
# Now download the image. If these were text files,
    # or other ascii types, just pass an empty string
    # for the second param ala stealStuff(file_name,'',base_url)
    stealStuff(file_name,"b",base_url)


]]>
Python Stringhttp://www.tkk7.com/nkjava/archive/2011/03/04/345716.html鑽夊師涓婄殑楠嗛┘鑽夊師涓婄殑楠嗛┘Fri, 04 Mar 2011 07:52:00 GMThttp://www.tkk7.com/nkjava/archive/2011/03/04/345716.htmlhttp://www.tkk7.com/nkjava/comments/345716.htmlhttp://www.tkk7.com/nkjava/archive/2011/03/04/345716.html#Feedback0http://www.tkk7.com/nkjava/comments/commentRss/345716.htmlhttp://www.tkk7.com/nkjava/services/trackbacks/345716.htmlPython-String-Function

瀛楃涓蹭腑瀛楃澶у皬鍐欑殑鍙樻崲錛?/strong>

    * S.lower()   #灝忓啓
    * S.upper()   #澶у啓
    * S.swapcase()   #澶у皬鍐欎簰鎹?br />     * S.capitalize()   #棣栧瓧姣嶅ぇ鍐?br />     * String.capwords(S) 
      #榪欐槸妯″潡涓殑鏂規(guī)硶銆傚畠鎶奡鐢╯plit()鍑芥暟鍒嗗紑錛岀劧鍚庣敤capitalize()鎶婇瀛楁瘝鍙樻垚澶у啓錛屾渶鍚庣敤join()鍚堝茍鍒頒竴璧?br />     * S.title()    #鍙湁棣栧瓧姣嶅ぇ鍐欙紝鍏朵綑涓哄皬鍐欙紝妯″潡涓病鏈夎繖涓柟娉?

瀛楃涓插湪杈撳嚭鏃剁殑瀵歸綈錛?

    * S.ljust(width,[fillchar]) 
      #杈撳嚭width涓瓧絎︼紝S宸﹀榻愶紝涓嶈凍閮ㄥ垎鐢╢illchar濉厖錛岄粯璁ょ殑涓虹┖鏍箋?br />     * S.rjust(width,[fillchar])    #鍙沖榻?br />     * S.center(width, [fillchar])    #涓棿瀵歸綈
    * S.zfill(width)   #鎶奡鍙樻垚width闀匡紝騫跺湪鍙沖榻愶紝涓嶈凍閮ㄥ垎鐢?琛ヨ凍

瀛楃涓蹭腑鐨勬悳绱㈠拰鏇挎崲錛?/strong>

    * S.find(substr, [start, [end]]) 
      #榪斿洖S涓嚭鐜皊ubstr鐨勭涓涓瓧姣嶇殑鏍囧彿錛屽鏋淪涓病鏈塻ubstr鍒欒繑鍥?1銆俿tart鍜宔nd浣滅敤灝辯浉褰撲簬鍦⊿[start:end]涓悳绱?br />     * S.index(substr, [start, [end]]) 
      #涓巉ind()鐩稿悓錛屽彧鏄湪S涓病鏈塻ubstr鏃訛紝浼?xì)杩斿洖涓涓繍琛屾椂閿欒
    * S.rfind(substr, [start, [end]]) 
      #榪斿洖S涓渶鍚庡嚭鐜扮殑substr鐨勭涓涓瓧姣嶇殑鏍囧彿錛屽鏋淪涓病鏈塻ubstr鍒欒繑鍥?1錛屼篃灝辨槸璇翠粠鍙寵竟綆楄搗鐨勭涓嬈″嚭鐜扮殑substr鐨勯瀛楁瘝鏍囧彿
    * S.rindex(substr, [start, [end]])
    * S.count(substr, [start, [end]])    #璁$畻substr鍦⊿涓嚭鐜扮殑嬈℃暟
    * S.replace(oldstr, newstr, [count])  
      #鎶奡涓殑oldstar鏇挎崲涓簄ewstr錛宑ount涓烘浛鎹㈡鏁般傝繖鏄浛鎹㈢殑閫氱敤褰㈠紡錛岃繕鏈変竴浜涘嚱鏁拌繘琛岀壒孌婂瓧絎︾殑鏇挎崲
    * S.strip([chars])
      #鎶奡涓墠鍚巆hars涓湁鐨勫瓧絎﹀叏閮ㄥ幓鎺夛紝鍙互鐞嗚В涓烘妸S鍓嶅悗chars鏇挎崲涓篘one
    * S.lstrip([chars])
    * S.rstrip([chars])
    * S.expandtabs([tabsize]) 
      #鎶奡涓殑tab瀛楃鏇挎崲娌$┖鏍鹼紝姣忎釜tab鏇挎崲涓簍absize涓┖鏍鹼紝榛樿鏄?涓?

瀛楃涓茬殑鍒嗗壊鍜岀粍鍚堬細(xì)

    * S.split([sep, [maxsplit]])
      #浠ep涓哄垎闅旂錛屾妸S鍒嗘垚涓涓猯ist銆俶axsplit琛ㄧず鍒嗗壊鐨勬鏁般傞粯璁ょ殑鍒嗗壊絎︿負(fù)絀虹櫧瀛楃
    * S.rsplit([sep, [maxsplit]])
    * S.splitlines([keepends])
      #鎶奡鎸夌収琛屽垎鍓茬鍒嗕負(fù)涓涓猯ist錛宬eepends鏄竴涓猙ool鍊鹼紝濡傛灉涓虹湡姣忚鍚庤屼細(xì)淇濈暀琛屽垎鍓茬銆?br />     * S.join(seq) #鎶妔eq浠h〃鐨勫簭鍒椻攢鈹瀛楃涓插簭鍒楋紝鐢⊿榪炴帴璧鋒潵

瀛楃涓茬殑mapping錛岃繖涓鍔熻兘鍖呭惈涓や釜鍑芥暟錛?/strong>

    * String.maketrans(from, to)
      #榪斿洖涓涓?56涓瓧絎︾粍鎴愮殑緲昏瘧琛紝鍏朵腑from涓殑瀛楃琚竴涓瀵瑰簲鍦拌漿鎹㈡垚to錛屾墍浠rom鍜宼o蹇呴』鏄瓑闀跨殑銆?br />     * S.translate(table[,deletechars])
      #浣跨敤涓婇潰鐨勫嚱鏁頒駭鍚庣殑緲昏瘧琛紝鎶奡榪涜緲昏瘧錛屽茍鎶奷eletechars涓湁鐨勫瓧絎﹀垹鎺夈傞渶瑕佹敞鎰忕殑鏄紝濡傛灉S涓簎nicode瀛楃涓詫紝閭d箞灝變笉鏀寔 deletechars鍙傛暟錛屽彲浠ヤ嬌鐢ㄦ妸鏌愪釜瀛楃緲昏瘧涓篘one鐨勬柟寮忓疄鐜扮浉鍚岀殑鍔熻兘銆傛澶栬繕鍙互浣跨敤codecs妯″潡鐨勫姛鑳芥潵鍒涘緩鏇村姞鍔熻兘寮哄ぇ鐨勭炕璇戣〃銆?

瀛楃涓茶繕鏈変竴瀵圭紪鐮佸拰瑙g爜鐨勫嚱鏁幫細(xì)

    * S.encode([encoding,[errors]])
      #鍏朵腑encoding鍙互鏈夊縐嶅鹼紝姣斿gb2312 gbk gb18030 bz2 zlib big5 bzse64絳夐兘鏀寔銆俥rrors榛樿鍊間負(fù)"strict"錛屾剰鎬濇槸UnicodeError銆傚彲鑳界殑鍊艱繕鏈?ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 鍜屾墍鏈夌殑閫氳繃codecs.register_error娉ㄥ唽鐨勫箋傝繖涓閮ㄥ垎鍐呭娑夊強(qiáng)codecs妯″潡錛屼笉鏄壒鏄庣櫧
    * S.decode([encoding,[errors]])

瀛楃涓茬殑嫻嬭瘯鍑芥暟錛岃繖涓綾誨嚱鏁板湪string妯″潡涓病鏈夛紝榪欎簺鍑芥暟榪斿洖鐨勯兘鏄痓ool鍊鹼細(xì)

    * S.startwith(prefix[,start[,end]])
      #鏄惁浠refix寮澶?br />     * S.endwith(suffix[,start[,end]])
      #浠uffix緇撳熬
    * S.isalnum()
      #鏄惁鍏ㄦ槸瀛楁瘝鍜屾暟瀛楋紝騫惰嚦灝戞湁涓涓瓧絎?br />     * S.isalpha()  #鏄惁鍏ㄦ槸瀛楁瘝錛屽茍鑷沖皯鏈変竴涓瓧絎?br />     * S.isdigit()  #鏄惁鍏ㄦ槸鏁板瓧錛屽茍鑷沖皯鏈変竴涓瓧絎?br />     * S.isspace() #鏄惁鍏ㄦ槸絀虹櫧瀛楃錛屽茍鑷沖皯鏈変竴涓瓧絎?br />     * S.islower() #S涓殑瀛楁瘝鏄惁鍏ㄦ槸灝忓啓
    * S.isupper() #S涓殑瀛楁瘝鏄惁渚挎槸澶у啓
    * S.istitle() #S鏄惁鏄瀛楁瘝澶у啓鐨?

瀛楃涓茬被鍨嬭漿鎹㈠嚱鏁幫紝榪欏嚑涓嚱鏁板彧鍦╯tring妯″潡涓湁錛?/strong>

    * string.atoi(s[,base])
      #base榛樿涓?0錛屽鏋滀負(fù)0,閭d箞s灝卞彲浠ユ槸012鎴?x23榪欑褰㈠紡鐨勫瓧絎︿覆錛屽鏋滄槸16閭d箞s灝卞彧鑳芥槸0x23鎴?X12榪欑褰㈠紡鐨勫瓧絎︿覆
    * string.atol(s[,base])  #杞垚long
    * string.atof(s[,base])  #杞垚float

*******************************************************************************************************************
Python瀛楃涓插湪杈撳嚭鏃剁殑瀵歸綈錛?/p>

  1. S.ljust(width,[fillchar])   

#杈撳嚭width涓瓧絎︼紝S宸﹀榻愶紝涓嶈凍閮ㄥ垎鐢╢illchar濉厖錛岄粯璁ょ殑涓虹┖鏍箋?/p>

  1. S.rjust(width,[fillchar]) #  

 

鍙沖榻?/p>

  1. S.center(width, [fillchar]) #  

 

涓棿瀵歸綈 S.zfill(width) #鎶奡鍙樻垚width闀匡紝騫跺湪鍙沖榻愶紝涓嶈凍閮ㄥ垎鐢?琛ヨ凍瀛楃涓蹭腑鐨勬悳绱㈠拰鏇挎崲錛?/p>

  1. S.find(substr, [start, [end]])   

#榪斿洖S涓嚭鐜皊ubstr鐨勭涓涓瓧姣嶇殑鏍囧彿錛屽鏋淪涓病鏈塻ubstr鍒欒繑鍥?1銆俿tart鍜宔nd浣滅敤灝辯浉褰撲簬鍦⊿[start:end]涓悳绱?/p>

  1. S.index(substr, [start, [end]])   

#涓巉ind()鐩稿悓錛屽彧鏄湪S涓病鏈塻ubstr鏃訛紝浼?xì)杩斿洖涓涓繍琛屾椂閿欒

  1. S.rfind(substr, [start, [end]])   

#榪斿洖S涓渶鍚庡嚭鐜扮殑substr鐨勭涓涓瓧姣嶇殑鏍囧彿錛屽鏋淪涓病鏈塻ubstr鍒欒繑鍥?1錛屼篃灝辨槸璇翠粠鍙寵竟綆楄搗鐨勭涓嬈″嚭鐜扮殑substr鐨勯瀛楁瘝鏍囧彿

  1. S.rindex(substr, [start, [end]])   
  2. S.count(substr, [start, [end]]) #  

 

璁$畻substr鍦⊿涓嚭鐜扮殑嬈℃暟

  1. S.replace(oldstr, newstr, [count])   

#鎶奡涓殑oldstar鏇挎崲涓簄ewstr錛宑ount涓烘浛鎹㈡鏁般傝繖鏄浛鎹㈢殑閫氱敤褰㈠紡錛岃繕鏈変竴浜涘嚱鏁拌繘琛岀壒孌婂瓧絎︾殑鏇挎崲

**************************************************************************************************



]]>
主站蜘蛛池模板: 久久亚洲美女精品国产精品| 69天堂人成无码麻豆免费视频| 巨波霸乳在线永久免费视频 | 337p日本欧洲亚洲大胆裸体艺术| 免费无遮挡无码永久在线观看视频| 亚洲精品无码激情AV| 亚洲天堂久久精品| 亚洲精品伦理熟女国产一区二区| 三级黄色免费观看| 永久免费AV无码国产网站| 最近免费中文字幕大全视频| 色视频色露露永久免费观看| 精品亚洲一区二区| 亚洲av日韩av无码黑人| 亚洲资源在线观看| 老司机午夜在线视频免费 | 亚洲色成人网站WWW永久四虎 | 四虎永久成人免费| 亚洲综合无码一区二区| 免费看一级高潮毛片| av无码久久久久不卡免费网站| 国产aⅴ无码专区亚洲av麻豆| 国产精品亚洲精品| 亚洲色大成网站www| 无码专区AAAAAA免费视频| 91频在线观看免费大全| 国产成人亚洲影院在线观看| 亚洲欧洲日韩国产一区二区三区| 亚洲aⅴ无码专区在线观看| 香港a毛片免费观看 | 99久久亚洲综合精品成人网| 高潮毛片无遮挡高清免费| 美女被cao免费看在线看网站| 成人免费无码精品国产电影| 久久精品国产亚洲av麻豆色欲 | 无码免费又爽又高潮喷水的视频 | 国产一区二区三区免费在线观看| 亚洲男人的天堂在线| 嫩草在线视频www免费看| 国产一区二区三区在线免费观看| 亚洲香蕉久久一区二区|