日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python一些常用方法_python 的一些常用方法

發布時間:2025/4/16 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python一些常用方法_python 的一些常用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.[代碼][Python]代碼

1.生成隨機數

02 import random #這個是注釋,引入模塊

03 rnd = random.randint(1,500)#生成1-500之間的隨機數

04

05 2.讀文件

06

07 f = open("c:\\1.txt","r")

08 lines = f.readlines()#讀取全部內容

09 for line in lines

10 print line

11 3.寫文件

12 f = open("c:\\1.txt","r+")#可讀可寫模式

13 f.write("123")#寫入字符串

14

15 4.正則表達式,讀取tomcat的日志并打印日期

16

17 import re

18 regx = "\d\d\d\d-\d\d-\d+"

19 f = open("c:\stdout.log","r")

20 i = 0

21 for str in f.readlines():

22 if re.search(regx,str):

23 Response.write(str+"
")

24 if i>10:break#由于是測試,只分析十行

25 i=i+1

26 f.close();

27

28 5.連接數據庫

29

30 import pgdb

31

32 conn = pgdb.connect

33

34 (host='localhost',databse='qingfeng',user='qingfeng',password='123')

35

36 cur = conn.cursor()

37

38 cur.execute("select * from dream")

39

40 print cur.rowcount

41

42 6.SAX處理xml:

43

44 import string

45 from xml.sax import saxlib, saxexts

46

47 class QuotationHandler(saxlib.HandlerBase):

48 """Crude sax extractor for quotations.dtd document"""

49

50 def __init__(self):

51 self.in_quote = 0

52 self.thisquote = ''

53

54 def startDocument(self):

55 print '--- Begin Document ---'

56

57 def startElement(self, name, attrs):

58 if name == 'quotation':

59 print 'QUOTATION:'

60 self.in_quote = 1

61 else:

62 self.thisquote = self.thisquote + '{'

63

64 def endElement(self, name):

65 if name == 'quotation':

66 print string.join(string.split(self.thisquote[:230]))+'...',

67 print '('+str(len(self.thisquote))+' bytes)\n'

68 self.thisquote = ''

69 self.in_quote = 0

70 else:

71 self.thisquote = self.thisquote + '}'

72

73 def characters(self, ch, start, length):

74 if self.in_quote:

75 self.thisquote = self.thisquote + ch[start:start+length]

76

77 if __name__ == '__main__':

78 parser = saxexts.XMLParserFactory.make_parser()

79 handler = QuotationHandler()

80 parser.setDocumentHandler(handler)

81 parser.parseFile(open("sample.xml"))

82 parser.close()

83

84

85 7.python的GUI模塊標準的是Tkinter,也有QT和MFC的模塊,有興趣的大家自己搜索下

86

87 import Tkinter

88

89 root=Tkinter.Tk()

90

91 my=Label(root,"Welcome to python's world")

92

93 my.pack()

94

95 root.mainloop()

總結

以上是生活随笔為你收集整理的python一些常用方法_python 的一些常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。