私はこのコードを持っています:
word = ["General William Shelton, said the system",
"which will provide more precise positional data",
"and that newer technology will provide more",
"Commander of the Air Force Space Command",
"objects and would become the most accurate metadata"]
交換したい:
Replace “the” with “THE”, “
Replace “William Shelton” with “AliBaba”
Replace “data” with “SAMSUNG”
出力は次のようになります。
General AliBaba,said THE system which will provide more precise
positional SAMSUNG and that newer technology will provide more
Commander of the Air Force Space Command objects and would become the
most accurate metadata
ありがとうございました!
私はこれを試しました:
rep_word = {"the":"THE", "William Shelton":"AliBaba", "system":"Samsung"}
replace = re.compile(r'\b(' + '|'.join(rep_word.keys()) + r')\b')
result = replace.sub(lambda x: rep_word[x.group()], word)
print result
しかし、私はこのエラーを受け取りました: TypeError: expected string or buffer