Python 替换列表中字符串中的字符,并为每个字符创建一个副本
我不确定是否有可能用sed做我需要的事情 我希望能够用列表中的另一个字符串替换文件中的一个字符串,并为每个版本创建一个单独的副本 例如,原始文件具有:Python 替换列表中字符串中的字符,并为每个字符创建一个副本,python,string,replace,sed,Python,String,Replace,Sed,我不确定是否有可能用sed做我需要的事情 我希望能够用列表中的另一个字符串替换文件中的一个字符串,并为每个版本创建一个单独的副本 例如,原始文件具有: apple dog orange 我想用不同单词的列表替换dog。i、 猫,兔子,鸭子。 这将创建原始文件的副本,每个副本都有不同的更改 File1.txt(原始文件)-apple dog orange File2.txt-苹果猫橙 File3.txt-苹果兔橙 File4.txt-苹果鸭橙 我已经能够使用Sed查找和替换单个文件: sed -
apple dog orange
我想用不同单词的列表替换dog
。i、 猫,兔子,鸭子。
这将创建原始文件的副本,每个副本都有不同的更改
File1.txt(原始文件)-apple dog orange
File2.txt-苹果猫橙
File3.txt-苹果兔橙
File4.txt-苹果鸭橙
我已经能够使用Sed查找和替换单个文件:
sed -i '' 's/dog/cat/g' *.html
我需要一种方法来替换列表中的内容,并创建每个内容的唯一副本
我在OSX上。正如我之前所说,sed可能不是这里要使用的工具,也许是Python?我愿意接受任何这样做的建议
谢谢您可以使用正则表达式使用python来完成
import re
import os
filename = '/path/to/filename.txt'
with open(filename, 'r') as f:
text = f.read()
names = ['cat', 'rabbit', 'duck']
for i, name in enumerate(names):
new_text = re.sub(r'dog', name, text)
base, ext = os.path.splitext(filename)
with open('{}_{}{}'.format(base, i, ext), 'w') as f:
f.write(new_text)
您可以使用正则表达式使用python实现这一点
import re
import os
filename = '/path/to/filename.txt'
with open(filename, 'r') as f:
text = f.read()
names = ['cat', 'rabbit', 'duck']
for i, name in enumerate(names):
new_text = re.sub(r'dog', name, text)
base, ext = os.path.splitext(filename)
with open('{}_{}{}'.format(base, i, ext), 'w') as f:
f.write(new_text)
你可以使用正则表达式
import re
in_file = 'File1.txt'
out_file = 'File2.txt'
with open(filename, 'r') as in_f:
s = f.read() # Reads whole file. Be careful for large files
s = re.sub('dog', 'cat', s)
with open(out_file, 'w') as out_f:
f.write(s)
您可以将整个内容包含在for
循环中,以进行任意数量的替换您可以使用regex
import re
in_file = 'File1.txt'
out_file = 'File2.txt'
with open(filename, 'r') as in_f:
s = f.read() # Reads whole file. Be careful for large files
s = re.sub('dog', 'cat', s)
with open(out_file, 'w') as out_f:
f.write(s)
您可以将整个内容包含在
for
循环中,以进行任意数量的替换只需使用字符串替换方法即可
import string
f = open("file1")
text = f.read()
list_of_words = ['cat','rabbit','duck']
num =2
for word in list_of_words:
new_text = string.replace(text,"dog",word,1)
f_new = open("file"+str(num),"w")
f_new.write(new_text)
f_new.close()
num +=1
输入:
文件1
输出:文件2:
apple cat orange
输出:文件3:
apple rabbit orange
输出:文件4:
apple duck orange
只需使用字符串替换方法即可完成
import string
f = open("file1")
text = f.read()
list_of_words = ['cat','rabbit','duck']
num =2
for word in list_of_words:
new_text = string.replace(text,"dog",word,1)
f_new = open("file"+str(num),"w")
f_new.write(new_text)
f_new.close()
num +=1
输入:
文件1
输出:文件2:
apple cat orange
输出:文件3:
apple rabbit orange
输出:文件4:
apple duck orange
非常糟糕的问题,学习如何编码的乐趣在于一路尝试和理解非常糟糕的问题,学习如何编码的乐趣在于一路尝试和理解我喜欢你如何确保它只是狗而不是狗。干得好!非常感谢(还有卡梅拉尼斯)。实际上,我需要它来替换,即使是在
狗鱼的情况下。这个想法是为了替换不同地区的URL的一部分。抱歉,我不是想这么早发布-这个想法是为了替换HTML文件中的URL的一部分:www.example.com/_en/page
到www.example.com/_fr/page
或www.example.com/_de/page
等@user2382925进行了更新,以消除单词限制,从而使其更有效替换所有的dog
,即使它们包含在另一个单词中。我喜欢你确保它只是dog
,而不是dogfish
。干得好!非常感谢(还有卡梅拉尼斯)。实际上,我需要它来替换,即使是在狗鱼的情况下。这个想法是为了替换不同地区的URL的一部分。抱歉,我不是想这么早发布-这个想法是为了替换HTML文件中的URL的一部分:www.example.com/_en/page
到www.example.com/_fr/page
或www.example.com/_de/page
等@user2382925进行了更新,以消除单词限制,从而使其更有效替换所有狗
,即使它们包含在另一个单词中。