Discuss / Python / 哪个大神知道 如何把单词中带有i 字母的全部替换为其他单词?

哪个大神知道 如何把单词中带有i 字母的全部替换为其他单词?

Topic source

with open('..test.txt', 'w') as w: w.write('hello, world, this my first Python text template. So excited that I made it!!!!I will keep working on this') with open('..test.txt', 'r') as w1: s = w1.readlines() with open('..test.txt', 'w') as w2: for i in s: w2.write(i.replace("I", "Yourname",2)) with open('..test.txt', 'a') as w3: w3.write(' 好好学习')

#桌面就自动生成 一个 test.txt的文件了

#哪个大神知道 如何把单词中带有i 字母的全部替换为其他单词?

我就比较蠢了...提一个蠢办法 以空格或者','结尾,当做一个单词,匹配单词中的'i'或者'I', 如果匹配到就直接替换

.replace()


  • 1

Reply