python替換文件中的指定行數(shù)技巧示例詳解
1. 背景描述
最近在寫一個后端項目,主要的操作就是根據(jù)用戶的前端數(shù)據(jù),在后端打開項目中的代碼文件,修改對應(yīng)位置的參數(shù),因為在目前的后端項目中經(jīng)常使用這個操作,所以簡單總結(jié)一下。
1. 文件路徑:./test.c
2. 文件內(nèi)容
……
case EPA:
chan_desc->nb_taps = 7;
chan_desc->Td = .410;
chan_desc->channel_length = (int) (2*chan_desc->sampling_rate*chan_desc->Td + 1 + 2/(M_PI*M_PI)*log(4*M_PI*chan_desc->sampling_rate*chan_desc->Td));
sum_amps = 0;
chan_desc->amps = (double *) malloc(chan_desc->nb_taps*sizeof(double));
chan_desc->free_flags=chan_desc->free_flags|CHANMODEL_FREE_AMPS ;
for (i = 0; i<chan_desc->nb_taps; i++) {
chan_desc->amps[i] = pow(10,.1*epa_amps_dB[i]);
sum_amps += chan_desc->amps[i];
}
for (i = 0; i<chan_desc->nb_taps; i++)
chan_desc->amps[i] /= sum_amps;
chan_desc->delays = epa_delays;
chan_desc->ricean_factor = 1;//待修改位置
chan_desc->aoa = 0;//待修改位置
chan_desc->random_aoa = 0;//待修改位置
chan_desc->ch = (struct complexd **) malloc(nb_tx*nb_rx*sizeof(struct complexd *));
chan_desc->chF = (struct complexd **) malloc(nb_tx*nb_rx*sizeof(struct complexd *));
chan_desc->a = (struct complexd **) malloc(chan_desc->nb_taps*sizeof(struct complexd *));
……2. 單行修改-操作步驟
讀取文件
使用python中的open()函數(shù)進行文件讀取,將數(shù)據(jù)存儲在緩沖區(qū)。
#1. 讀取文件 path='./test.c' with open(path, 'r') as file: file_content = file.read()
查找文件替換位置
以查找chan_desc->ricean_factor = 1;//待修改位置為例,查找這句話的起點和終點。
## 注:此步驟需要import re
#2. 查找文件替換位置
start_index=file_content.find('chan_desc->ricean_factor = ')#起點
end_index=file_content.find('chan_desc->aoa = ',start_index)#終點
if end_index==-1 or start_index==-1:
print('未找到待修改位置')
#此時得到的兩個指針,分別指向了待修改位置的起點和終點,如下圖所示:
設(shè)置替換文件內(nèi)容
假設(shè)目前只修改這一行的參數(shù),
#3. 設(shè)置替換文件內(nèi)容 ricean_factor=3#假設(shè)這是要修改的參數(shù)信息 updata_content=file_content[:start_index]#獲取這行代碼之前的內(nèi)容 update_content+='chan_desc->ricean_factor = '+str(ricean_factor)+';//待修改位置'#修改這行代碼 update_content+=file_content[end_index:]#獲取這行代碼之后的內(nèi)容 #此時得到的update_content就是修改后的完整文件內(nèi)容,只修改了ricean_factor這一行的值
寫入文件
同樣使用python中的open函數(shù)。
#4. 寫入文件
if update_content!="":#如果修改內(nèi)容不為空
with open(path, 'w') as file:#w表示覆蓋寫入,之前的內(nèi)容都會被覆蓋
file.write(update_content)總代碼
整體的代碼如下所示:
import re
#1. 讀取文件
path='./test.c'
with open(path, 'r') as file:
file_content = file.read()
#2. 查找文件替換位置
start_index=file_content.find('chan_desc->ricean_factor = ')#起點
end_index=file_content.find('chan_desc->aoa = ',start_index)#終點
if end_index==-1 or start_index==-1:
print('未找到待修改位置')
#3. 設(shè)置替換文件內(nèi)容
ricean_factor=3#假設(shè)這是要修改的參數(shù)信息
updata_content=file_content[:start_index]#獲取這行代碼之前的內(nèi)容
update_content+='chan_desc->ricean_factor = '+str(ricean_factor)+';//待修改位置'#修改這行代碼
update_content+=file_content[end_index:]#獲取這行代碼之后的內(nèi)容
#4. 寫入文件
if update_content!="":#如果修改內(nèi)容不為空
with open(path, 'w') as file:#w表示覆蓋寫入,之前的內(nèi)容都會被覆蓋
file.write(update_content)3. 多行修改-操作步驟
多行修改思路
多行修改有兩種修改思路,如果修改部分比較集中,則可直接替換一整塊的字符串內(nèi)容,如果修改部分較為分散,則需要單獨查找修改位置,然后再分別進行替換。
多行修改-整塊替換
try:
with open(file_path, "r") as file:
file_content = file.read()
except Exception as e:
return str(e)
# 設(shè)置改寫內(nèi)容
updated_content = ""
# 查找修改
start_index_1 = file_content.find("start_sentence")#要確保查找元素的唯一性
end_index_1 = file_content.find("end_sentence",start_index_1,)
if start_index_1 == -1 or end_index_1 == -1:
print("未找到待修改位置")
return -1
#
updated_content = file_content[:start_index_1]#獲取這行代碼之前的內(nèi)容
updated_content += "start_sentence和end_sentence之間的sentence_1;\n"
updated_content += "start_sentence和end_sentence之間的sentence_2;\n"
updated_content +=file_content[end_index_1:]
##此時updated_content就是修改后的完整文件內(nèi)容
if updated_content != "":
with open(file_path, "w") as file:
file.write(updated_content)
else:
print("修改失敗")
return -1多行修改-局部替換
try:
with open(file_path, "r") as file:
file_content = file.read()
except Exception as e:
return str(e)
# 設(shè)置改寫內(nèi)容
updated_content = ""
# 查找修改
start_index_1 = file_content.find("start_sentence_1")#要確保查找元素的唯一性
end_index_1 = file_content.find("end_sentence_1",start_index_1,)
start_index_2 = file_content.find("start_sentence_2",end_index_1)
end_index_2 = file_content.find("end_sentence_2",start_index_2,)
start_index_3 = file_content.find("start_sentence_3",end_index_2)
end_index_3 = file_content.find("end_sentence_3",start_index_3,)
start_index_4 = file_content.find("start_sentence_4",end_index_3)
end_index_4 = file_content.find("end_sentence_4",start_index_4,)
if (
start_index_1 == -1
or end_index_1 == -1
or start_index_2 == -1
or end_index_2 == -1
or start_index_3 == -1
or end_index_3 == -1
or start_index_4 == -1
or end_index_4 == -1
):
print("未找到待修改位置")
return -1
#
updated_content = file_content[:start_index_1]#獲取這行代碼之前的內(nèi)容
updated_content += "start_sentence_1和end_sentence_1之間的內(nèi)容"
updated_content +=file_content[end_index_1:start_index_2]
updated_content += "start_sentence_2和end_sentence_2之間的內(nèi)容"
updated_content +=file_content[end_index_2:start_index_3]
updated_content += "start_sentence_3和end_sentence_3之間的內(nèi)容"
updated_content +=file_content[end_index_3:start_index_4]
updated_content += "start_sentence_4和end_sentence_4之間的內(nèi)容"
updated_content += file_content[end_index_4:]
##此時updated_content就是修改后的完整文件內(nèi)容
if updated_content != "":
with open(file_path, "w") as file:
file.write(updated_content)
else:
print("修改失敗")
return -1以上就是python替換文件中的指定行數(shù)技巧示例詳解的詳細內(nèi)容,更多關(guān)于python替換文件指定行的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中強制關(guān)閉線程與協(xié)程與進程方法
python使用中多線程、多進程、多協(xié)程使用是比較常見的。那么如果在多線程等的使用,我們這個時候我們想從外部強制殺掉該線程請問如何操作?這篇文章帶你介紹,感興趣的同學可以參考閱讀2023-03-03
Python+OpenCV 實現(xiàn)簡單的高斯濾波(推薦)
這篇文章主要介紹了Python+OpenCV 實現(xiàn)簡單的高斯濾波,在文中需要注意的是,這里我沒有特判當sigma = 0的時候的情況,具體實現(xiàn)過程跟隨小編一起看看吧2021-09-09
Python如何自動生成環(huán)境依賴包requirements
這篇文章主要介紹了Python如何自動生成環(huán)境依賴包requirements問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
使用PyQt5設(shè)計GUI實現(xiàn)程序圖形界面設(shè)計
當我們學會如何在pycharm中配置pyqt5設(shè)計GU之后,那么本文來帶你熟悉PyQt5設(shè)計GUI流程并為程序設(shè)計圖形界面,設(shè)計一個屬于自己的GUI2021-08-08
python使用wmi模塊獲取windows下的系統(tǒng)信息 監(jiān)控系統(tǒng)
Python用WMI模塊獲取Windows系統(tǒng)的硬件信息:硬盤分區(qū)、使用情況,內(nèi)存大小,CPU型號,當前運行的進程,自啟動程序及位置,系統(tǒng)的版本等信息。2015-10-10

