python學習

2015-01-19

1.print是格式化打印

   -print "hello world"

   -print  “ %d is year"  %  year

   -strHello = "the length of (%s) is %d" %('laowangpython',len('laowangpython'))
    print strHello

   -print ("%.3s " % ("python"))
     pyt

   -print ("%.*s" % (4,"python"))
    pyth

   print 自動換行  :print 會自動在行末加上回車,如果不需回車,只需在print語句的結尾添加一個逗號”,“,就可以改變它的行爲。

<span class="kw1">  >>> for i in range(0,6):
      print (i,)</span>

 

2. if __name__ == '__main__': 的解析

    每個.py文件都是一個模塊,每個模塊都有一個__name__的屬性,在本模塊內,它默認是'__main__',但是如果在另外一個.py文件B裏面import 該模塊,那麼調用這個模塊的時 候裏面的__name__就不是'__main__'了。。所以爲了在B裏面調用這個模塊的時候,僅僅把整個代碼import過來,調用裏面的函數,但是不執行模塊裏面需要執行的部分,所以使用這樣的if語句,把該模塊裏面需要執行的部分放進去。。僅僅在該模塊執行的時候執行,不影響其他調用該模塊的.py文件

 

3.  或與非: or, and, not

 

4. int(year)   轉化爲整型

 

5. from __future__ import division  輸入浮點數

6. # _*_ coding: utf-8 _*_     or  #coding=utf-8  解決中文解析

7.import os

   from os import listdir

   import os as o

   from oswalk import *   這樣就可以使用oswalk文件裏面的所有函數了,直接調用

 

8.os模塊處理文件和目錄

  listdir(”/")  列出目錄下的文件和目錄       windows下面使用d:\\ 進行分區指定

  walk  列出目錄下整個信息,包括子目錄

   os.walk,  os.path.walk ---後者產生3個列表信息:   路徑,目錄列表,文件列表 

      eg: for path, dirs., files in os.path.walk("/")

                        print root, dirs., files

 

9.文件操作:

   f=open(path,mode )   讀寫模式:r只讀,r+讀寫,w新建(會覆蓋原有文件),a追加,b二進制文件.常用模式
   f.read([size]) size未指定則返回整個文件,如果文件大小>2倍內存則有問題.f.read()讀到文件尾時返回""(空字串) ----read()之後,指針會移動到當前位置

   f.readline() 返回一行

   f.readline([size]) 返回包含size行的列表,size 未指定則返回全部行

   for line in f: print line #通過迭代器訪問

   f.write("hello\n") #如果要寫入字符串以外的數據,先將他轉換爲字符串.

  f.tell() 返回一個整數,表示當前文件指針的位置(就是到文件頭的比特數).

  f.seek(偏移量,[起始位置])

     用來移動文件指針

      偏移量:單位:比特,可正可負

     起始位置:0-文件頭,默認值;1-當前位置;2-文件尾

f.close() 關閉文件

 

9-2:python按行讀取文件:

      1. while 1:                效率低,節省內存

              line=f.readline()

             if not line: break

       2. import fileinput               效率低

           for line infileinput.input("sample.txt"):       

       3. while 1:        效率高

               lines=f.readlines()

               if not lines: break

               for line in lines:

        4. file對象

              for line in open(file,mode)

 

 

10 出錯:IndentationError:expected an indented block錯誤解決----需要縮進

 

11. python 處理html裏面的轉義字符:

      抓網頁數據經常遇到例如&gt;或者&nbsp;這種HTML轉義符,抓到字符串裏很是煩人。

     比方說一個從網頁中抓到的字符串

   html = '&lt;abc&gt;'

     用Python可以這樣處理:

  import HTMLParser
  html_parser = HTMLParser.HTMLParser()
  txt = html_parser.unescape(html) #這樣就得到了txt = '<abc>'

    如果還想轉回去,可以這樣:

  import cgi
  html = cgi.escape(txt) # 這樣又回到了 html = '&lt;abc&gt'

 

12 python處理命令行參數

     1.sys.argv[] 是所有的命令行參數列表,sys.argv[1]是腳本名字

     2.對於其他的參數解析可以使用getopt模塊

         import sys,getopt

         try:  

             opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help""output="])  ----1.除腳本名之後的命令行所有選項和參數;2.短命令集合  3.長命令集合

        except getopt.GetoptError:  

            # print help information and exit:

 

        函數返回兩個列表:opts 和args 。opts 爲分析出的格式信息。args 爲不屬於格式信息的剩餘的命令行參數。

        opts 是一個兩元組的列表。每個元素爲:( 選項, 參數) 。如果沒有附加參數則爲空串'' 。
        eg: '-h -o file --help --output=out file1 file2'

          在分析完成後,opts 應該是: [('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')]
          而args 則爲: ['file1', 'file2']

         接着使用循環來處理opts的合法性:

         for o, a in opts:  

             if o in ("-h""--help"):  

                    usage()  

                    sys.exit()  

          if o in ("-o""--output"):  

                    output = a 

 

       

 13. cmd 模塊: http://blog.sina.com.cn/s/blog_ac9fdc0b0101nd3d.html

    

class PyCDC(cmd.Cmd):
    def __init__(self):       ---默認基類創建,類的成員變量在此函數內聲明,可以賦初值
        cmd.Cmd.__init__(self)
        self.CDROM="e:\\training\\2014\\Python"
        self.CDDIR='e:\\training\\2014\\Python\\script'
        self.prompt="(PyCDC)>"                                             -----------cmd的promt是命令行前綴
        self.intro=''' PyCDC0.5 usage:                                    -----------cmd的intro是命令行的最初提示
              dir dirname #the save/search dir, default is ../
              walk filename # keep the cdc info to filename, use *.cdc
              find  keywork # search all .cdc files in the save/search dir, input the lines including the keyword
              ?  #seach  ------顯示命令選擇界面EOF walk  dir find 等這些函數(help_walk, do_walk 這些函數都是成對出現的)
              EOF # exit the system
              '''
            
    def help_EOF(self): 
        print "exit the programme"
    def do_EOF(self, line):
        sys.exit()
       
    def help_walk(self):
        print "walk cd and export inot *.cdc"
    def do_walk(self,filename):

        if filename == "":
            filename=raw_input("input the cdc filename::")
            print "walk the cdc to : %s" % filename
            osWalk(self.CDROM, os.path.join(self.CDDIR,filename))  ----成員函數裏面的參數使用類的屬性成員self.xx

                                                                                                        ----成員函數裏面調用其他模塊的函數

 

     cdc=PyCDC()

      cdc.cmdloop()- --這個函數執行cmd模塊的命令界面,可以根據函數名裏面的關鍵詞(do_walk)walk選擇,來執行do_walk函數

   

     

14. 類的使用 

      filename="xxx"

    類成員函數使用:cdc.do_walk(filename) ,第一個參數self省略

 

15 in是非常強大 匹配操作, 可以判斷字符串,list ,元組,字典中的元素是否存在,對於字符串就是匹配操作了

    a='12345'

    if  '1' in a

 

16.所有python軟件都可以通過python setup.py install來安裝

 

17.利用chardet來將文本解析爲utf8格式,支持中文  *******字符編碼問題*************

1 import chardet
2 def _smartcode(stream):
3     """smart recove stream into UTF-8
4     """
5     ustring = stream
6     codedetect = chardet.detect(ustring)["encoding"]
7     print codedetect
8     try:
9         print ustring
10         ustring = unicode(ustring, codedetect)
11         print ustring
12         return "%s %s"%("",ustring.encode('utf8'))
13     except:
14         return u"bad unicode encode try!"

 

18.python文檔化開發---開發規範

     epydoc文檔生成工具 (

在cygwin下安裝 svn co https://epydoc.svn.sourceforge.net/svnroot/epydoc/trunk/epydoc epydoc

 python setup.py install

 然後使用 epydoc --config cdc.cfg 可以在api docs目錄(mv *docs docs,否則沒法打開)看到生成的html文件

不同系統上的安裝:http://web.mit.edu/course/6/6.863/src/epydoc-3.0.1/doc/manual-install.html

(windows上面安裝.exe之後,在python目錄下面的script目錄會生成epydoc.pyw界面)

       http://blog.csdn.net/largetalk/article/details/6950435

http://blog.csdn.net/agoago_2009/article/details/28415301

 

19:命名規範

      命令儘量使用動賓短語

      目錄 文件名全部小寫

       類名使用首字母大寫單詞串(eg:WikiNames)

       全局變量使用全大寫字串(eg:CAPWORD)

      函數使用混合大小寫串(eg:mixCase)

       內部變量 常數,全部小寫

       eg:

        1 from mypacket import MyClass
        2 GLOBALVAR="是也乎"
        3 def doInsert(arg):
        4         MyClass.myFunc(GLOBALVAR)

 

 

20.對於一個列表hello=['a','b','c'],想要它們換行輸出  print '\n'.join(hello)

21. python包的安裝, python  setup.py  install

22. 使用pip 工具來安裝python 包:

      1. wget https://pypi.python.org/packages/source/p/pip/pip-1.2.1.tar.gz

         easy_install pip

      2. curl -0 https://raw.github.com/pypa/pip/master/contrib/get-pip.py

          sudo python get-pip.py

     pip install Markdown

     pip install -U Markdown

     pip uninstall Markdown

     pip search "Markdown"

 

 

 

 

 

 

 

 

 

 

 

***針對cd管理:

http://tech.sina.com.cn/c/2003-12-05/25149.html

http://www.nicomsoft.com/products/ocr/features/類似通過抓取指定介質的信息進行外部管理的軟件是一個大類,cdc dialog

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章