亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python 正則表達式替換

瀏覽:98日期:2022-07-05 14:29:33

問題描述

最近遇到一個正則表達式替換的問題

time數據里面的每條數據前面都有[0]= [1]= [2]= [3]=這個索引:

['time']={[0]={['status']=true,['ac']=1,['bg']=2},[1]={['status']=true,['ac']=1,['bg']=2},[2]={['status']=true,['ac']=1,['bg']=2},}

因為一些原因前面的索引沒了,只能用正則來加上,問題是time里面的數據數量是不一樣的

['time']={{['status']=true,['ac']=1,['bg']=2},}['time']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},}['time']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},}

有沒有方法自動在前面加順序的[0]= [1]= [2]= [3]=

補充:

錯誤的數據是在一起的,而且time里面的數據順序不相同,如下:

['time1']={{['status']=true,['ac']=1,['bg']=2},},['time2']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},},['time3']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},}

想改成:

['time1']={[0]={['status']=true,['ac']=1,['bg']=2},},['time2']={[0]={['status']=true,['ac']=1,['bg']=2},[1]={['status']=true,['ac']=1,['bg']=2},},['time3']={[0]={['status']=true,['ac']=1,['bg']=2},[1]={['status']=true,['ac']=1,['bg']=2},[2]={['status']=true,['ac']=1,['bg']=2},}

問題解答

回答1:

>>> import re>>> s=’['time']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},}’>>> n=0>>> def repl(m): global n rslt=’[%d]=%s’%(n,m.group(0)) n+=1 return rslt>>> p=re.compile(r’{[^{}]+},’)>>> p.sub(repl,s)’['time']={[0]={['status']=true,['ac']=1,['bg']=2},[1]={['status']=true,['ac']=1,['bg']=2},[2]={['status']=true,['ac']=1,['bg']=2},}’回答2:

i = 0def func(x): global i s = ’[%d]=%s’ % (i,x) i += 1 return s import rea = ’['time']={{['status']=true,['ac']=1,['bg']=2},{['status']=true,['ac']=1,['bg']=2},}’print re.sub(’{['status'’,lambda m:func(m.group(0)),a)

寫的不好,見笑了

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产精品东北一极毛片 | 国产真实搭讪系列 | 精品欧美高清一区二区免费 | 久久www免费人成高清 | 欧美午夜精品一区二区三区 | 国产综合在线视频 | 国产r级在线观看 | 香蕉视频网址 | 久久亚洲精品成人综合 | 大陆一级毛片免费高清 | 精品欧美成人bd高清在线观看 | 777久久| 免费看黄色的视频 | 国产高清免费不卡观看 | 国产在线一区在线视频 | 国产亚洲精品福利在线 | 中文字幕专区高清在线观看 | 国产精品短篇二区 | 男人免费看片 | xxx本日| 尤物视频在线观看免费 | 国产小视频网址 | 国产一级大片在线观看 | 久色亚洲| 国模无水印一区二区三区 | 高清视频欧美性生活 | 欧美成年黄网站色高清视频 | 日本亚洲一区二区三区 | 欧美三黑人一级特黄曰皮 | 可以免费观看的一级毛片 | 91轻吻| 欧美日本黄色 | 在线观看免费黄色小视频 | 九九热视频在线观看 | 国产亚洲精品久久久久久久 | 日韩欧国产精品一区综合无码 | 性情视频在线高清免费 | 制服丝袜自拍偷拍 | 另类欧美日韩 | 欧美日韩性视频一区二区三区 | 中文字幕日韩一区 |