2016-08-18 71 views
1

我有一個字符串,我想在其中以#之後的數字替換它們以創建增量。Python:字符串中的字符串中的字符數相互相關

例如:

rawString = 'MyString1_test##_edit####' 

for x in xrange(5): 
    output = doConvertMyString(rawString) 
    print output 

MyString1_test01_edit0001 
MyString1_test02_edit0002 
MyString1_test03_edit0003 
MyString1_test04_edit0004 
MyString1_test05_edit0005 

假設的#的數目不是固定的,並且rawString是僅包含string.ascii_letters + string.digits + '_' + '#的用戶輸入,如何做呢?

這是到目前爲止我的測試:小於10

rawString = 'MyString1_test##_edit####' 
incrDatas = {} 
key = '#' 
counter = 1 

for x in xrange(len(rawString)): 
    if rawString[x] != key: 
     counter = 1 
     continue 
    else: 
     if x > 0: 
      if rawString[x - 1] == key: 
       counter += 1 
      else: 
       pass 
       # ??? 
+0

,什麼是問題? –

+0

你還沒有提供'doConvertMyString'函數的細節,是因爲你還沒有試圖寫它? – Andrew

+0

「#」號碼是動態的還是固定的?是否可以有兩個以上的'#'符號? –

回答

2

您可以在re.sub更換使用zfill墊的#塊任何金額。 #+正則表達式匹配1個或多個#符號。 m.group()代表找到的正則表達式的匹配,因此,我們用代替所有# s,將x轉換爲填充字符串的數量爲0 s,因爲匹配中有#

import re 
rawString = 'MyString1_test##_edit####' 
for x in xrange(5): 
    output = re.sub(r"#+", lambda m: str(x+1).zfill(len(m.group())), rawString) 
    print output 

the demo結果:

MyString1_test01_edit0001 
MyString1_test02_edit0002 
MyString1_test03_edit0003 
MyString1_test04_edit0004 
MyString1_test05_edit0005 
+1

完美的作品,謝謝! – UKDP

-2
test_string = 'MyString1_test##_edit####' 

def count_hash(raw_string): 
    str_list = list(raw_string) 
    hash_count = str_list.count("#") + 1 
    for num in xrange(1, hash_count): 
     new_string = raw_string.replace("####", "000" + str(num)) 
     new_string = new_string.replace("##", "0" + str(num)) 
     print new_string 

count_hash(test_string) 

這是一個有點笨重,而且只適用於#計數,但似乎你想要做什麼。 編輯:通過「只能」我的意思是,你會得到額外的字符插入

EDIT2固定號碼#符號:修改代碼

+3

它實際上並不是。在第二次迭代中沒有要替換的'#'。 – DeepSpace

+1

好點。哎呦 – Andrew

0

如何這 -

rawString = 'MyString1_test##_edit####' 
splitString = rawString.split('_') 

for i in xrange(10): # you may put any count 
    print '%s_%s%02d_%s%04d' % (splitString[0], splitString[1][0:4], i, splitString[2][0:4], i,) 
0

你可以試試這個天真的(也許不是最有效的)解決方案。它假定'#'的數量是固定的。

rawString = 'MyString1_test##_edit####' 

for i in range(1, 6): 
    temp = rawString.replace('####', str(i).zfill(4)).replace('##', str(i).zfill(2)) 
    print(temp) 

>> MyString1_test01_edit0001 
    MyString1_test02_edit0002 
    MyString1_test03_edit0003 
    MyString1_test04_edit0004 
    MyString1_test05_edit0005 
1

下面的代碼的rawString轉換爲一個格式字符串,在列表理解使用groupby找到散列組。每次散列運行都會轉換爲格式指令,以打印適當寬度的零填充整數,非散列運行會簡單地連接在一起。

此代碼適用於Python 2.6及更高版本。

from itertools import groupby 

def convert(template): 
    return ''.join(['{{x:0{0}d}}'.format(len(list(g))) if k else ''.join(g) 
     for k, g in groupby(template, lambda c: c == '#')]) 

rawString = 'MyString1_test##_edit####' 
fmt = convert(rawString) 
print(repr(fmt)) 

for x in range(5): 
    print(fmt.format(x=x)) 

輸出

'MyString1_test{x:02d}_edit{x:04d}' 
MyString1_test00_edit0000 
MyString1_test01_edit0001 
MyString1_test02_edit0002 
MyString1_test03_edit0003 
MyString1_test04_edit0004 
相關問題