python重要的函數代碼塊

來源:互聯網
上載者:User

標籤:

  注意:現在python3.3.5中print列印語句有了新寫法:

1. python指定產生隨機數

>>> import random>>> >>> #正確測print寫法>>> rand = random.randint(1,500)>>> print (rand)343>>> #錯誤的print文法:>>> print randSyntaxError: invalid syntax>>> 

2.  #python對檔案操作

1.產生隨機數          import random    #這個是注釋,引入模組          rnd = random.randint(1,500)#產生1-500之間的隨機數 2.讀檔案          f = open("c:\\1.txt","r")         lines = f.readlines()#讀取全部內容         for line in lines                 print line3.寫檔案        f = open("c:\\1.txt","r+")#可讀可寫入模式        f.write("123")#寫入字串 4.Regex,讀取tomcat的日誌並列印日期      import re     regx = "\d\d\d\d-\d\d-\d+"     f = open("c:\stdout.log","r")     i = 0     for str in f.readlines():        if re.search(regx,str):             Response.write(str+"<br>")              if i>10:break#由於是測試,只分析十行              i=i+1     f.close(); 5.串連資料庫 import pgdb conn = pgdb.connect (host=‘localhost‘,databse=‘qingfeng‘,user=‘qingfeng‘,password=‘123‘)         cur = conn.cursor()         cur.execute("select * from dream")         print cur.rowcount 6.SAX處理xml:       import string      from xml.sax import saxlib, saxexts       class QuotationHandler(saxlib.HandlerBase):          """Crude sax extractor for quotations.dtd document"""           def __init__(self):                  self.in_quote = 0                  self.thisquote = ‘‘           def startDocument(self):              print ‘--- Begin Document ---‘           def startElement(self, name, attrs):              if name == ‘quotation‘:                  print ‘QUOTATION:‘                  self.in_quote = 1              else:                  self.thisquote = self.thisquote + ‘{‘           def endElement(self, name):              if name == ‘quotation‘:                  print string.join(string.split(self.thisquote[:230]))+‘...‘,                  print ‘(‘+str(len(self.thisquote))+‘ bytes)\n‘                  self.thisquote = ‘‘                  self.in_quote = 0              else:                  self.thisquote = self.thisquote + ‘}‘           def characters(self, ch, start, length):              if self.in_quote:                  self.thisquote = self.thisquote + ch[start:start+length]       if __name__ == ‘__main__‘:          parser  = saxexts.XMLParserFactory.make_parser()          handler = QuotationHandler()          parser.setDocumentHandler(handler)          parser.parseFile(open("sample.xml"))          parser.close()  7.python的GUI模組標準的是Tkinter,也有QT和MFC的模組,有興趣的大家自己搜尋下         import Tkinter         root=Tkinter.Tk()         my=Label(root,"Welcome to python‘s world")         my.pack()         root.mainloop()

 

python重要的函數代碼塊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.