如何在python中的特定单词之后替换下一个单词

时间:2018-10-05 05:03:03

标签: python string

我有一个像下面这样的字符串。在这里,我想在诸如'%(db_user)s'和'%(db_passsword)s'之类的特定单词之后替换下一个立即数,但是我可以在字符串中搜索的单词是< strong>-db-user和--db-passwords ,因为上述内容将替换为值。

输入:

 "cd scripts &&   bash setup.sh  --client-name %(client_name)s --is-db-auth-enabled %(is_db_auth_enabled)s --db-user '%(db_user)s' --db-password '%(db_password)s' "

输出:

 "cd scripts &&   bash setup.sh  --client-name %(client_name)s --is-db-auth-enabled %(is_db_auth_enabled)s --db-user '***' --db-password '****' "

因此,请为我提供一个函数,该函数将提供一个单词数组和一个字符串,该字符串将替换提供的单词中的下一个单词。

3 个答案:

答案 0 :(得分:3)

这会有所帮助-

import re

def char_index(sentence, word_index): 
    sentence = re.split('(\s)',sentence) #Parentheses keep split characters 
    return len(''.join(sentence[:word_index*2]))

def print_secure_message(msg):
    secure_words = ['--db-user', '--db-password']
    # Removing extra white spaces within string
    msg = re.sub(' +', ' ', msg)
    cpy_msg = msg.split(" ")
    for word in secure_words:
        # Getting index of the word's first characters
        t = re.search(word, msg)
        # Getting index of the next word of the searched word's
        word_index = cpy_msg.index(word)+2;
        index= char_index(msg, word_index)
        print(t.end(), word_index, index)
        msg = msg[0:t.end() + 1] + "'****'" + msg[index - 1:]
    print(''.join(msg))

答案 1 :(得分:1)

您可以在此处使用insert。您将.split()的初始string设置为list。然后,您将insert置于要搜索的单词的index之后的位置。在' '.join()之后,list返回到string

s = "cd scripts &&   bash setup.sh  --client-name %(client_name)s --is-db-auth-enabled %(is_db_auth_enabled)s --db-user '%(db_user)s' --db-password '%(db_password)s' "

s = s.split()
a = '***'
b = '****'

s.insert((s.index('--db-user')+1), a)
s.insert((s.index('--db-password')+1), b)
s = ' '.join(s)
print(s)
# cd scripts && bash setup.sh --client-name %(client_name)s --is-db-auth-enabled %(is_db_auth_enabled)s --db-user *** '%(db_user)s' --db-password **** '%(db_password)s'

答案 2 :(得分:0)

  

一个函数,其中我将提供一个单词数组和一个字符串,该字符串将替换下一个单词。

使用常规字符串处理

以下解决方案利用Python的function someOtherFunction() { let obj: someObjType; if (Math.random() > .5) { obj = { prop: 'a', value: 'some String' }; } else { obj = { prop: 'b', value: 200 }; }; // Do Something using obj.prop and obj.value. someFunction(obj) }; 方法,这种方法非常适合在格式良好的字符串中查找内容,而无需使用regexp。

list.index

这是通过首先将输入字符串拆分成单词,然后为argmap中的每个键/值对找到键的索引,并将def replace_cmdargs(cmdargs, argmap): words = cmdargs.split(' ') for arg, value in argmap.iteritems(): index = words.index(arg) argname = words[index + 1].replace('%(', '').replace(')s', '').replace("'", '').replace('"', '') words[index + 1] = words[index + 1] % {argname: value} return ' '.join(words) 上的现有单词替换为相应的值来实现的。

我们可以如下使用index + 1函数

replace_cmdargs

注意:这是假设字符串格式正确,即键和要替换的值之间只有一个空格,并且始终有一个对应的字符串值。

利用Python的内置字符串格式

由于我们已经有一个格式正确的字符串,其中包含格式指令,因此我们当然也可以使用Python的内置字符串格式 运算符,不需要额外的功能:

cmdargs = "--db-user '%(db_user)s' --db-password '%(db_password)s'"
replace_cmdargs(cmdargs, {
        '--db-user': 'MYUSER',
        '--db-password': 'MYPASS'
    })

=> "--db-user 'MYUSER' --db-password 'MYPASS'"
相关问题