代码之家  ›  专栏  ›  技术社区  ›  Rashmi Jain

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

  •  3
  • Rashmi Jain  · 技术社区  · 7 年前

    我有一根像下面这样的线。在这里,我想在一个特定的词之后替换下一个直接的词,比如, 但是我可以在字符串中搜索的单词是 --db user和--db密码

    输入:

     "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 回复  |  直到 7 年前
        1
  •  3
  •   Rashmi Jain    7 年前

    这会有帮助的-

    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))
    
        2
  •  1
  •   vash_the_stampede    7 年前

    你可以用 insert 在这里。你会的 .split() string list . 那你会的 插入 一个接一个 index ' '.join() 这个 列表 回到一个 一串

    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'
    
        3
  •  0
  •   miraculixx    7 年前

    一个函数,其中我将提供一个单词数组和一个字符串,它将替换下一个单词到那些提供的单词。

    下面的解决方案利用了Python的 list.index

    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

    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'"
    

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

    利用Python内置的字符串格式

    因为我们已经有了一个格式良好的字符串,其中包含格式指令,所以我们当然也可以使用Python内置的字符串格式 操作员,无需额外功能:

    cmdargs % { 'db_user': 'MYUSER', 'db_password': 'MYPASS'}
    => "--db-user 'MYUSER' --db-password 'MYPASS'"