用自身的大写字母替换文本中的特定单词

时间:2019-06-14 13:43:42

标签: python python-3.x string replace

好,所以这应该不算困难,但是我在画一个空白。因此,该程序的想法是找到仅在文本中出现一次的单词,然后将其大写。这是完整的代码:

from collections import Counter
from string import punctuation

path = input("Path to file: ")
with open(path) as f:
    word_counts = Counter(word.strip(punctuation) for line in f for word in line.replace(")", " ").replace("(", " ")
                      .replace(":", " ").replace("", " ").split())

wordlist = open(path).read().replace("\n", " ").replace(")", " ").replace("(", " ").replace("", " ")

unique = [word for word, count in word_counts.items() if count == 1]

for word in unique:
    text = wordlist
    text.replace(word, str(word.upper()))

print(text)

它只打印常规文本,而没有进行任何修改。

事实上,我知道第一部分起作用,这只是最后的for循环,这给我带来了麻烦。

知道我在搞砸吗?

2 个答案:

答案 0 :(得分:4)

替换此行

    .slidecontainer {
      width: 100%;
    }

    .slider {
      -webkit-appearance: none;
      width: 100px;
      height: 15px;
      border-radius: 5px;  
      background: #d3d3d3;
      outline: none;
      -webkit-transition: .2s;
      transition: opacity .2s;
      position: relative;
      top: 130px;
      opacity:0;
    }

    .slider:hover {
      opacity: 1;
    }

    .slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      border-radius: 50%;
      background: #4CAF50;
      cursor: pointer;
    }

    .slider::-moz-range-thumb {
      width: 25px;
      height: 25px;
      border-radius: 50%;
      background: #4CAF50;
      cursor: pointer;
    }

使用

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <div class="slidecontainer">
      <input type="range" min="0" max="100" value="100" class="slider" id="volume">
    </div>
    <img id="Coffee" src="https://static.wixstatic.com/media/38d0c2_f65cba752ac3460da7a56896d1195284~mv2.png"
    height="100" width="100">


    <audio id="CoffeeAudio" loop>
       <source src="//music.wixstatic.com/preview/38d0c2_02a2ea74abbc49a29ae7bdfabd1091d0-128.mp3" type="audio/wav" />
    </audio>

text.replace(word, str(word.upper())) 不会修改原始字符串实例。

答案 1 :(得分:1)

您应该将其分配回文本。

findViewById(R.id.myTitle)