<tfoot id="uuuu0"><noscript id="uuuu0"></noscript></tfoot>
  • <tfoot id="uuuu0"></tfoot>
  • <nav id="uuuu0"></nav>
  • <tfoot id="uuuu0"><noscript id="uuuu0"></noscript></tfoot>
    • 精品丝袜人妻久久久久久,人妻系列在线无码,日韩av东京社区男人的天堂,伊人久久大香线蕉在观看,成年女人喷潮毛片免费播,99在线精品视频免费

      新聞資訊

      洞悉互聯(lián)網(wǎng)前沿資訊,探尋網(wǎng)站優(yōu)化規(guī)律。

      Python字符串和文件操作常用函數(shù)分析 - 資訊報道 - 本溪紅海

      發(fā)表日期:2016-12-04 11:19:43   文章編輯:紅海小編   瀏覽次數(shù):0

      本文實例分析了Python字符串和文件操作常用函數(shù)。分享給大家供大家參考。具體如下:
       


      本溪紅海傳媒廣告——本溪網(wǎng)站建設(shè),卓越領(lǐng)導(dǎo)者!(m.reviseyourwebsite.com)


      # -*- coding: UTF-8 -*-

      '''

      Created on 2010-12-27

      @author: sumory

      '''

      import itertools

      def a_containsAnyOf_b(seq,aset):

      '''判斷seq中是否含有aset里的一個或者多個項

      seq可以是字符串或者列表

      aset應(yīng)該是字符串或者列表'''

      for item in itertools.ifilter(aset.__contains__,seq):

      return True

      return False

      def a_allIn_b(seq,aset):

      '''判斷seq中的所有項是否都在aset里

      seq可以是字符串或者列表

      aset應(yīng)該是字符串或者列表'''

      for item in seq:

      if item not in aset:

      return False

      return True

      def a_containsAll_b(seq,aset):

      '''判斷seq是否包含aset里的所有項

      seq可以是字符串或者列表

      aset應(yīng)該是字符串或者列表

      任何一個set對象a,a.difference(b)等價于a-set(b),即返回a中所有不屬于b的元素'''

      return not set(aset).difference(seq)

      import string

      #生成所有字符的可復(fù)用的字符串

      sumory_allchars=string.maketrans('','')

      def makefilter(keep):

      '''返回一個函數(shù),此函數(shù)接受一個源字符串作為參數(shù)

      并返回字符串的一個部分拷貝

      此拷貝只包括keep中的字符,keep必須是一個普通的字符串

      調(diào)用示例:makefilter('abca ')('abcdefgh ijkal cba')

      在后面的字符串中保留前面出現(xiàn)的字符 abc a cba

      '''

      #按照sumory_allchars規(guī)則剔除sumory_allchars字符串中的keep里的字符

      #這里得到keep在sumory_allchars的補集

      deletechars=sumory_allchars.translate(sumory_allchars,keep)

      #生成并返回需要的過濾函數(shù)(作為閉包)

      def realdelete(sourseStr):

      return sourseStr.translate(sumory_allchars,deletechars)

      return realdelete

      def list_removesame(list):

      '''刪除list中的重復(fù)項'''

      templist=[]

      for c in list:

      if c not in templist:

      templist.append(c)

      return templist

      def re_indent(str,numberofspace):

      '''

      縮進

      將字符串str中按換行符劃分并在每句前加上numberofspace個space

      再組合成字符串'''

      spaces=numberofspace*' '

      lines=[spaces+line.strip() for line in str.splitlines()]

      return 'n'.join(lines)

      def replace_strby_dict(sourseStr,dict,marker='"',safe=False):

      '''使用字典替換源字符串中的被marker包裹的相應(yīng)值'''

      #如果safe為True,那么字典中沒找到key時不替換

      if safe:

      def lookup(w):

      return dict.get(w,w.join(marker*2))

      #w.join(marker*2)用marker包裹w

      #如果safe為False,那么字典中沒找到key時拋異常

      #若將dict[w]換為dict.get(w)則沒找到時返回None

      else:

      def lookup(w):

      return dict[w]

      #根據(jù)marker切分源字符串

      splitparts=sourseStr.split(marker)

      #取出切分后的奇數(shù)項

      #因為切分后,列表中源字符串中marker包裹的項肯定位于基數(shù)部位

      #就算是'"first"s is one'這樣的字符串也是如此

      #分割后的第0項為空串,第1項為first

      splitparts[1::2]=map(lookup,splitparts[1::2])

      return ''.join(splitparts)

      def simply_replace_strby_dict(sourseStr,dict,safe=True):

      '''根據(jù)dict內(nèi)容替換sourseStr原串中$標(biāo)記的子字符串

      dict= {'name':'sumory','else':'default'}

      $$5 -> $5

      $else -> default

      ${name}'s method -> sumory's method

      '''

      style=string.Template(sourseStr)

      #如果safe,在dict中找不到的話不會替換,照樣保留原串

      if safe:

      return style.safe_substitute(dict)

      #false,找不到會拋異常

      else:

      return style.substitute(dict)

      ##################################################

      def scanner(object,linehandler):

      '''用linehandler方法遍歷object的每一項'''

      for line in object:

      linehandler(line)

      def printfilelines(path):

      '''讀取path路徑下的文件屏逐行打印'''

      fileobject=open(path,'r')#open不用放到try里

      try:

      for line in fileobject:

      print(line.rstrip('n'))

      finally:

      fileobject.close()

      def writelisttofile(path,ilist):

      fileobject=open(path,'w')

      try:

      fileobject.writelines(ilist)

      finally:

      fileobject.close()

      import zipfile

      def listzipfilesinfo(path):

      z=zipfile.ZipFile(path,'r')

      try:

      for filename in z.namelist():

      bytes=z.read(filename)

      print('File:%s Size:%s'%(unicode(filename, 'cp936').decode('utf-8'),len(bytes)))

      finally:

      z.close()

      import os,fnmatch

      def list_all_files(root,patterns='*',single_level=False,yield_folders=False):

      '''列出目錄(或者及其子目錄下的文件)'''

      #分割模式到列表

      patterns=patterns.split(';')

      for path,subdirs,files in os.walk(root):

      if yield_folders:

      files.extend(subdirs)

      files.sort()

      for name in files:

      for pat in patterns:

      if fnmatch.fnmatch(name, pat):

      yield '/'.join(unicode(os.path.join(path,name),'cp936').split('/'))

      break

      if single_level:

      break

      def swapextensions(root,before,after):

      if before[:1]!='.':

      before='.'+before

      extensionlen=-len(before)

      if after[:1]!='.':

      after='.'+after

      for path,subdirs,files in os.walk(root):

      for oldfile in files:

      if oldfile[extensionlen:]==before:

      oldfile=os.path.join(path,oldfile)

      newfile=oldfile[:extensionlen]+after

      os.rename(oldfile, newfile)


      更多網(wǎng)站建設(shè)相關(guān)知識交流可聯(lián)系本溪紅海傳媒(m.reviseyourwebsite.com)(專注于本溪紅海傳媒廣告,本溪網(wǎng)站建設(shè)本溪網(wǎng)頁設(shè)計本溪做網(wǎng)站,網(wǎng)絡(luò)品牌策略建議)。


      希望本文所述對大家的Python程序設(shè)計有所幫助。

      一鍵分享到:
      返回列表

      News

      行業(yè)資訊

      提供網(wǎng)站建設(shè)相關(guān)資訊、互聯(lián)網(wǎng)行業(yè)資訊、網(wǎng)站設(shè)計知識、空間域名郵箱、網(wǎng)站解決方案、常見問題、簽約新聞等

      做網(wǎng)站要有創(chuàng)新意識

      2016-11-18 23:32:44

      做網(wǎng)站要有創(chuàng)新意識

      立異于軟件業(yè),就比方錢于社會人,立異不是全能的,沒有立異卻是萬萬不能的。可是立異也象錢相同,不是說有就有,乃至不能說跟自個努力有決議聯(lián)系。所以許多時分,媒體...

      網(wǎng)站開發(fā)市場正面臨著一場大“減肥”的運動

      2016-11-18 23:33:21

      網(wǎng)站開發(fā)市場正面臨著一場大“減...

      導(dǎo)讀:本溪紅海傳媒網(wǎng)站12月4日在&ldquo;特別報道&rdquo;專欄中撰文指出:伴隨著上個世紀(jì)90年代經(jīng)濟的高速增加,很多的商用軟件層出不窮,而現(xiàn)在如此紛繁復(fù)雜的商...

      網(wǎng)站建設(shè)公司所面臨的困境

      2016-11-18 23:36:31

      網(wǎng)站建設(shè)公司所面臨的困境...

      教學(xué)構(gòu)造單一   人才才能弱化   如今,我國軟件人才的培育首要依托規(guī)范院校的學(xué)歷教學(xué),集中在本科期間。據(jù)統(tǒng)計,我國當(dāng)時軟件從業(yè)人員約有40萬...

      網(wǎng)站建設(shè)公司的明天

      2016-11-18 23:37:58

      網(wǎng)站建設(shè)公司的明天

      在前不久的殺毒廠商降價熱潮后,有人提出這個疑問?國內(nèi)通用軟件廠商在將來的存活點終究在哪里?之所以用&ldquo;存活點&rdquo;這個詞匯來形容,是因為跟著對于個人...

      2017年網(wǎng)站建設(shè)人才需求增加

      2016-11-18 23:39:36

      2017年網(wǎng)站建設(shè)人才需求增加...

      《復(fù)興軟件工業(yè)舉動大綱》日前正式發(fā)動。2002年9月18日,國務(wù)院辦公廳轉(zhuǎn)發(fā)了《復(fù)興軟件工業(yè)舉動大綱(2002年至2005年)》(國辦發(fā)〔2002〕47號)(以下簡稱《舉動大綱...

      精品丝袜人妻久久久久久
      <tfoot id="uuuu0"><noscript id="uuuu0"></noscript></tfoot>
    • <tfoot id="uuuu0"></tfoot>
    • <nav id="uuuu0"></nav>
    • <tfoot id="uuuu0"><noscript id="uuuu0"></noscript></tfoot>