2016-08-18 72 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 
相关问题