主要記錄初學(xué)者常用的一些代碼:

          1.獲取當(dāng)前時(shí)間
          # -*- coding: gbk -*-
          import os
          import time
          import shutil

          today = time.strftime('%Y-%m-%d 星期%w')
          now = time.strftime('%H:%M:%S')
          >>>
          2008-05-07 星期3
          20:28:14

          2.os.path常見屬性
          os.path.sep :路徑分隔符 。以前老愛用'\\'? ,后來用'/'。
          os.path.altsep:(根目錄,不是很確定,我用來做根目錄。反正在windows表現(xiàn)是'/')
          os.path.curdir: 當(dāng)前目錄
          os.path.pardir: 父目錄

          3.判斷是否是指定文件類型
          File.rsplit('.',1)[-1] == type
          當(dāng)然也可以寫成File.split('.')[-1:][0] == type




          posted on 2008-05-07 20:44 -274°C 閱讀(15423) 評(píng)論(15)  編輯  收藏 所屬分類: python


          FeedBack:
          # re: python代碼總結(jié)
          2008-05-07 21:20 | -274°C
          4.中文字符

          >>> aa = "a是中國(guó)人"
          >>> print aa
          a是中國(guó)人
          >>> print aa[1]
          Ê
          >>> print aa[1:3]

          >>> c = unicode(aa,"gb2312")
          >>> print c[1]

          >>> print len(c)
          5
          >>> print len(aa)
          9  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-07 21:30 | -274°C
          5.python中對(duì)字符串排序:
          >>> s = "string"
          >>> l = list(s)
          >>> l.sort()
          >>> s = "".join(l)
          >>> s
          'ginrst'
            回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-07 22:45 | -274°C
          6.BASE64對(duì)字符串編碼和解碼

          a= "this is a teat"
          b = base64.encodestring(a)
          print b
          >>>
          dGhpcyBpcyBhIHRlYXQ=
          print base64.decodestring(b)
          >>>this is a teat

            回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-10 19:56 | java_he
          7.獲取當(dāng)前路徑
          os.getcwd()
          改變當(dāng)前路徑
          os.chdir(r"c:/")  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-10 22:53 | java_he
          8.修改文件名稱
          os.rename("bbb.txt","ccc.txt")
          如果ccc.txt已經(jīng)存在,則有異常拋出

            回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-10 22:56 | java_he
          8.文件夾改名
          os.rename("aaa","ccc")
          如果ccc已經(jīng)存在,則有異常拋出   回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-19 15:23 | java_he

          1.打印出xml文件的內(nèi)容

          from xml.dom import minidom
          xmldoc = minidom.parse('binary.xml')
          print xmldoc
          print xmldoc.toxml()

          2.判斷字符串以什么結(jié)尾和開頭

          string2.upper().startswith("EVEN")
          string2.upper().endswith("EVEN")

          3.比較時(shí)候以大,小寫來比較,字符串本身并未發(fā)生改變
          >>> print string2
          Odd or even
          >>> print string2.lower()
          odd or even
          >>> print string2.upper()
          ODD OR EVEN
          >>> print string2
          Odd or even
          >>>

          4.從url解析xml

          >>> import urllib
          >>> usock = urllib.urlopen('http://www.aygfsteel.com/JAVA-HE/category/19871.html/rss')
          >>> xmldoc = minidom.parse(usock)
          >>> usock.close()
          >>> print xmldoc.toxml()

          5.把string解析為xml

          >>> from xml.dom import minidom
          >>> contents = "<grammar><ref id='bit'><p>0</p><p>1</p></ref></grammar>"
          >>> xmldoc = minidom.parseString(contents)
          >>> print xmldoc.toxml()
          <?xml version="1.0" ?><grammar><ref id="bit"><p>0</p><p>1</p></ref></grammar>

          以前做AJAX 用到xml,python 使用在這方面是非常簡(jiǎn)單的。至少對(duì)使用者來講屏蔽了許多繁瑣的代碼。

          6.python 2.5 后 ,集合

          >>> b = set()
          >>> b.add(1)
          >>> b.add(2)
          >>> b.add(3)
          >>> c = set()
          >>> c.add(3)
          >>> c.add(4)
          >>> c.add(5)
          >>> d = b.difference(c)
          >>> print d
          set([1, 2])
          >>> print c.difference(b)
          set([4, 5])
          >>> print c.union(b)
          set([1, 2, 3, 4, 5])
          >>> print b.union(c)
          set([1, 2, 3, 4, 5])
          >>> print b.intersection(c)
          set([3])

          difference求不同 union求并集 infference 交集

          7.操作系統(tǒng)版本
          >>> import os
          >>> print os.name
          nt
          >>> import sys
          >>> print sys.platform
          win32
          >>> print sys.getwindowsversion()
          (5, 1, 2600, 2, 'Service Pack 2')
          >>>

          8.正則表達(dá)式獲取文件列表

          import glob
          # 生成當(dāng)前路徑下所有文件的列表
          a = glob.glob('*')
          print a
          # 生成當(dāng)前路徑下所有擴(kuò)展名為gif的文件列表。
          a = glob.glob('*.gif')

          9.python 2.4 后,參數(shù)可以函數(shù)的實(shí)例
          >>> def hehe(tt):
          return 'hehe'+tt()

          >>> def test():
          return 'test'

          >>> test = hehe(test)
          >>> print test
          hehetest

          10.range和xrange
          for i in range(0, 100):
          print i

          for i in xrange(0, 100):
          print i

          這兩個(gè)輸出的結(jié)果都是一樣的,實(shí)際上有很多不同,range會(huì)直接生成一個(gè)list對(duì)象:

          a = range(0,100)
          print type(a)
          print a
          print a[0], a[1]

          而xrange則不會(huì)直接生成一個(gè)list,而是每次調(diào)用返回其中的一個(gè)值

          a = xrange(0,100)
          print type(a)
          print a
          print a[0], a[1]

          所以xrange做循環(huán)的性能比range好,尤其是返回很大的時(shí)候!

          11.python里任何對(duì)象都可以print

          用 print 打印一個(gè)對(duì)象的時(shí)候,實(shí)際上會(huì)調(diào)用這個(gè)對(duì)象的__str__函數(shù)。
          所以 print 不僅僅是可以用來打印字符串和數(shù)字的。
          class A:
          def __init__(self):
          self.a = 1

          def __str__(self):
          return str(self.a)

          a = A()
          print a

          輸出:
          1

          12.兩種遍歷目錄對(duì)比

          >>> files = os.listdir(".")
          >>> for f in files:
          print "." + os.sep + f

          上面這種已經(jīng)用過了。下面是一種遞歸遍歷的:

          for root, dirs, files in os.walk("."):
          for name in files:
          print os.path.join(root,name)  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-05-20 10:47 | java_he
          1.對(duì)象拷貝:

          import copy

          a = [[1],[2],[3]]
          b = copy.copy(a)

          print "before", "=>"
          print a
          print b

          # modify original
          a[0][0] = 0
          a[1] = None

          print "after", "=>"
          print a
          print b

          before =>
          [[1], [2], [3]]
          [[1], [2], [3]]
          after =>
          [[0], None, [3]]
          [[0], [2], [3]]

            回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-06-14 17:18 | -274°C
          10. 獲取文件CRC

          from ctypes import *
          import binascii

          def getFileCRC(_path):
          try:
          blocksize = 1024 * 64
          f = open(_path,"rb")
          str = f.read(blocksize)
          crc = 0
          while(len(str) != 0):
          crc = binascii.crc32(str, crc)
          str = f.read(blocksize)
          f.close()
          except:
          klog.error("get file crc error!")
          return 0
          return c_uint(crc).value  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-10-07 14:04 |
          為何運(yùn)行不了  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-10-07 21:05 | 274
          @曲

          代碼都是運(yùn)行過的。注意縮進(jìn)。  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-11-12 09:51 | amanda
          how to get the current wiki page's url?  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2008-12-12 18:16 |
          不錯(cuò)噢,多加油!  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2009-08-06 18:41 | zngsai
          @-274&#176;C
          我的怎么是這個(gè):
          >>> aa = 'woshi中國(guó)人'
          >>> aa
          'woshi\xd6\xd0\xb9\xfa\xc8\xcb'  回復(fù)  更多評(píng)論
            
          # re: python代碼總結(jié)
          2009-08-06 18:41 | zngsai
          @-274&#176;C
          我的怎么是這個(gè):
          >>> aa = 'woshi中國(guó)人'
          >>> aa
          'woshi\xd6\xd0\xb9\xfa\xc8\xcb'  回復(fù)  更多評(píng)論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊(cè)

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916102
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 潮安县| 伊川县| 亚东县| 宝坻区| 广河县| 白城市| 富顺县| 原平市| 太康县| 资中县| 麟游县| 刚察县| 密山市| 德州市| 南乐县| 鹤山市| 武陟县| 田阳县| 资溪县| 清原| 昭觉县| 莱西市| 梅河口市| 平陆县| 来凤县| 福安市| 舟曲县| 上栗县| 营山县| 威宁| 阳江市| 包头市| 台前县| 洱源县| 库尔勒市| 托克托县| 辉南县| 开化县| 瑞昌市| 静安区| 黄石市|