⑴ gitpython如何修改文件內容不影響格式
三種方法
_弧⑿薷腦募絞?
_ef alter(file,old_str,new_str):
?
?"""
_婊晃募械淖址?
?:param file:文件名
?:param old_str:就字元串
?:param new_str:新字元串
?:return:
?
?"""
_ile_data = ""
?
_ith open(file, "r", encoding="utf-8") as f:
?
_or line in f:
?
_f old_str in line:
?
_ine = line.replace(old_str,new_str)
?
_ile_data += line
?
_ith open(file,"w",encoding="utf-8") as f:
?
_.write(file_data)
?
_lter("file1", "09876", "python")
?
__言募諶鶯鴕薷牡哪諶菪吹叫攣募薪寫媧⒌姆絞?
?
?2.1 python字元串替換的方法,修改文件內容
?
_mport os
?
_ef alter(file,old_str,new_str):
?
?"""
?
_婊壞淖址吹揭桓魴碌奈募校緩蠼募境攣募奈次募拿?
?
?:param file: 文件路徑
?
?:param old_str: 需要替換的字元串
?
?:param new_str: 替換的字元串
?
?:return: None
?
?"""
?
_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
?
_or line in f1:
?
_f old_str in line:
?
_ine = line.replace(old_str, new_str)
?
_2.write(line)
?
_s.remove(file)
?
_s.rename("%s.bak" % file, file)
?
_lter("file1", "python", "測試")
?
?2.2 python 使用正則表達式 替換文件內容 re.sub 方法替換
?
_mport re,os
?
_ef alter(file,old_str,new_str):
?
_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
?
_or line in f1:
?
_2.write(re.sub(old_str,new_str,line))
?
_s.remove(file)
?
_s.rename("%s.bak" % file, file)
⑵ 求Python讀取文件後寫入內容替換原內容的辦法
先將內容 讀取到內存中修改,然後使用w模式直接覆蓋原文件。
如果文件較大,可以分割寫入一個新文件,然後將原文件刪除,將新文件重命名為原文件。
⑶ Python批量修改文本文件內容的方法
Python批量修改文本文件內容的方法
Python批量替換文件內容,支持嵌套文件夾
import os
path="./"
for root,dirs,files in os.walk(path):
for name in files:
#print name
if name.endswith(".html"):
#print root,dirs,name
filename=root+"/"+name
f=open(filename,"r")
filecontent=""
line=f.readline()
while line:
l=line.replace(":/arcgis_js_api","/arcgisapi")
filecontent=filecontent+l
line=f.readline()
f.close()
f=file(filename,"w")
f.writelines(filecontent)
f.close()
關於本文給大家介紹的Python批量修改文本文件內容的方法
⑷ python里怎樣替換,修改文本內容
當我們讀取文件中內容後,如果想要修改文件中的某一行或者某一個位置的內容,在python中是沒有辦法直接實現的,如果想要實現這樣的操作只能先把文件所有的內容全部讀取出來,然後進行匹配修改後寫入到新的文件中。
實例代碼如下所示:
備註:
1. 舊文件的內容
hello,world
yanyan is good girl
Good day is good day
2. 新文件在代碼執行後的內容
hello,world
yanyan is good girl
hello,yanyan
3. 需要注意的是許可權的問題,對於舊文件必須要有讀取許可權,對於新的文件必須要有寫入許可權
⑸ Python如何將文件夾中的所有txt文件的內容替換
下面是我寫的,供參考:
import os
path = r'D:Desktope'
files = list(filter(lambda file:file[-4:]=='.txt',os.listdir(path)))
for file in files:
with open(path+os.sep+file,'r+') as f:
data = f.read()
data.replace('wo','我')
f.write(data)
⑹ python 或 批處理 替換文件中的內容
這個用sed就可以了:
sed -i 's/version=.*/version=0/' config.ini
如果有多個ini文件:
sed -i 's/version=.*/version=0/' *.ini
另外如果是windows系統,沒有自帶sed命令。可以到這里下載:
http://gnuwin32.sourceforge.net/packages/sed.htm
⑺ python如何替換txt文件某一列的值
所在系的這一列的值,找出一行的規律 :
性別+多個空格/TAB符+(替換目標)+多個空格/TAB符
a='文件路徑+文件名.txt','r+'
b=a.read().replace('多個空格/TAB符+替換目標+多個空格/TAB符','替換值')
a='文件路徑+文件名.txt','w+'
a.write(b)
a.close()
#例子:
a='abcdcdghkl'
b=a.replace('cd','ef')
#b最後的結果為abcdefghkl
⑻ python文本內容替換
這樣編寫:
fa=open("A.txt","r")
ta=fa.readlines()
fb=open("B.txt","r")
tb=fb.readlines()
tb[2:-9]=ta
fa.close()
fb.close()
fb=open("B.txt","w")
fb.writelines(tb)
fb.close()