我有2个文件src和dest
#cat src
lundi,mardi,mercredi,jeudi
# cat dest
janvier fevrier
mars avril mai
juillet aout
septembre octobre
使用python,我想用文件src的内容替换文件dest中的字符串“mai”。结果将是
# cat dest
janvier fevrier
mars avril lundi,mardi,mercredi,jeudi
juillet aout
septembre octobre
谢谢你的帮助
我尝试这些脚本,但它是否定的
1-
import os, sys
with open("src","r") as s:
with open("dst","w") as d:
for ligne in dst:
sligne=ligne.rstrip().split(" ")
for n in sline:
sline=mai
dst.str.replace("mai","src")
2-
d = open("dst","w")
s = open("src","r")
data=s.read()
s.close()
for n in dst:
data = data.replace("mai","s")
d.write(data)
d.close()
答案 0 :(得分:4)
string.replace
)。 *可选,但非常鼓励
答案 1 :(得分:0)
with open("src",'r') as file:
src = file.read()
file.close()
with open("dest",'r') as file:
dest = file.read()
file.close()
dest.replace('mai',src)
with open("dest",'w') as file:
file.write(dest)
file.close()
答案 2 :(得分:0)
谢谢大家,
我解决了我的问题。这是我的主张
with open("src","r") as file1:
src = file1.read()
src = src.rstrip()
with open("dst","r") as file2:
dst=file2.read()
resultat=dst.replace('mai',src)
with open("dst","w") as file2:
file2.write(resultat)