将python3.7降为3.5_python3.7降至3.5【python cookbook】python访问子字符串
訪問子字符串最簡單的的方式是使用切片
afiled=theline[3:8]但一次只能取一個子字符串
如果還要考慮字段的長度struct.unpack可能更合適
importstruct
#得到一個5字節(jié)的字符串跳過三字節(jié)得到兩個8字節(jié)的字符串以及其余部分
baseformat="5s3x8s8s"
#theline超出的長度也由這個base-format確定
numremain=len(theline)-struct.calcsize(baseformat)
#用合適的s或者x字段完成格式然后unpack
format="%s%ds"%(baseformat,numremain)
l,s1,s2,t=struct.unpack(format,theline)#test
>>>theline="numremain=len(theline)-struct.calcsize(baseformat)"
>>>numremain=len(theline)-struct.calcsize(baseformat)
>>>format="%s%ds"%(baseformat,numremain)
>>>format
'5s3x8s8s30s'
>>>l,s1,s2,t=struct.unpack(format,theline)
>>>l
'numre'
>>>s1
'n=len('
>>>s2
'theline)'
>>>t
'-struct.calcsize(baseformat)'
如果獲取固定字長的數(shù)據(jù),可以利用帶列表推導(dǎo)(LC)的切片方法
www.002pc.com認(rèn)為此文章對《python3.7降至3.5【python cookbook】python訪問子字符串》說的很在理。
pieces=[theline[k:k+n]forkinxrange(0,len(theline),n)]如果想把數(shù)據(jù)切成指定長度的列用帶LC的切片方法比較容易實現(xiàn)
cuts=[8,14,20,26,30]
pieces=[theline[i,j]forijinzip([0]+cuts,cuts+[None])]在LC中調(diào)用zip,返回的是一個列表每項形如cuts[k],cuts[k+1]
第一項和最后一項為(0,cuts[0])(cuts[len(cuts)-1],None)
將以上代碼片段封裝成函數(shù)deffields(baseformat,theline,lastfield=False):
#theline超出的長度也有這個base-format確定
#(通過struct.calcsize計算切片的長度)
numremain=len(theline)-struct.calcsize(baseformat)
#用合適的s或者x字段完成格式然后unpack
format="%s%d%s"%(baseformat,numre
下邊這個是使用memoizing機(jī)制的版本
deffields(baseformat,theline,lastfield=False,_cache={}):
#生成鍵并嘗試獲得緩存的格式字符串
key=baseformat,len(theline),lastfield
format_cache.get(key)
ifformatisNone:
#m沒有緩存的格式字符串創(chuàng)建并緩存
numremain=len(theline)-struct.calcsize(baseformat)
_cache[key]=format="%s%d%s"%(
baseformat,numremain,lastfieldand"s"or"x")
returnstruct.unpack(format,theline)cookbook上說的這個比優(yōu)化之前的版本快30%到40%不過如果這里不是瓶頸部分,沒必要使用這種方法
使用LC切片函數(shù)
defsplit_by(theline,n,lastfield=False):
#切割所有需要的片段
pieces=[theline[k:k+n]forkinxrange(0,len(theline),n)]
#弱最后一段太短或不需要,丟棄
ifnotlastfieldandlen(pieces[-1]
pieces.pop()
returnpiecesdefsplit_at(theline,cuts,lastfield=False):
#切割所有需要的片段
pieces=[theline[i,j]forijinzip([0]+cuts,cuts+[None])]
#若不需要最后一段丟棄
ifnotlastfield:
pieces.pop()
returnpieces
使用生成器的版本
defsplit_at(the_line,cuts,lastfield=False):
last=0
forcutincuts:
yieldthe_line[last:cut]
last=cut
iflastfield:
yieldthe_line[last:]
defsplit_by(the_line,n,lastfield=False):
returnsplit_at1(the_line,xrange(n,len(the_line),n),lastfield)zip()的用法
zip([iterable,...])Thisfunctionreturnsalistoftuples,wherethei-thtuplecontainsthei-thelementfromeachoftheargumentsequencesoriterables.Thereturnedlististruncatedinlengthtothelengthoftheshortestargumentsequence.Whentherearemultipleargumentswhichareallofthesamelength,zip()issimilartomap()withaninitialargumentofNone.Withasinglesequenceargument,itreturnsalistof1-tuples.Withnoarguments,itreturnsanemptylist.
Theleft-to-rightevaluationorderoftheiterablesisguaranteed.Thismakespossibleanidiomforclusteringadataseriesinton-lengthgroupsusingzip(*[iter(s)]*n).
zip()inconjunctionwiththe*operatorcanbeusedtounzipalist:>>>x=[1,2,3]
>>>y=[4,5,6]
>>>zipped=zip(x,y)
>>>zipped
[(1,4),(2,5),(3,6)]
>>>x2,y2=zip(*zipped)
>>>x==list(x2)andy==list(y2)
True
>>>x2
(1,2,3)
>>>y2
(4,5,6)
生成器的用法參見這篇博客/content/5987587.html
更多:python3.7降至3.5【python cookbook】python訪問子字符串
https://www.002pc.comhttps://www.002pc.com/python/5654.html
你可能感興趣的python,cookbook,字符串,訪問
No alive nodes found in your cluster
0踩
賞
0 贊
總結(jié)
以上是生活随笔為你收集整理的将python3.7降为3.5_python3.7降至3.5【python cookbook】python访问子字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器搬迁方案_数据中心机房改造搬迁ID
- 下一篇: python apply_async函数