最近在寫一個後端專案,主要的操作就是根據使用者的前端資料,在後端開啟專案中的程式碼檔案,修改對應位置的引數,因為在目前的後端專案中經常使用這個操作,所以簡單總結一下。
1. 檔案路徑:./test.c
2. 檔案內容
……
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 *));
……
#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('未找到待修改位置')
#此時得到的兩個指標,分別指向了待修改位置的起點和終點,如下圖所示:
#3. 設定替換檔案內容
ricean_factor=3#假設這是要修改的引數資訊
updata_content=file_content[:start_index]#獲取這行程式碼之前的內容
update_content+='chan_desc->ricean_factor = '+str(ricean_factor)+';//待修改位置'#修改這行程式碼
update_content+=file_content[end_index:]#獲取這行程式碼之後的內容
#此時得到的update_content就是修改後的完整檔案內容,只修改了ricean_factor這一行的值
#4. 寫入檔案
if update_content!="":#如果修改內容不為空
with open(path, 'w') as file:#w表示覆蓋寫入,之前的內容都會被覆蓋
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. 設定替換檔案內容
ricean_factor=3#假設這是要修改的引數資訊
updata_content=file_content[:start_index]#獲取這行程式碼之前的內容
update_content+='chan_desc->ricean_factor = '+str(ricean_factor)+';//待修改位置'#修改這行程式碼
update_content+=file_content[end_index:]#獲取這行程式碼之後的內容
#4. 寫入檔案
if update_content!="":#如果修改內容不為空
with open(path, 'w') as file:#w表示覆蓋寫入,之前的內容都會被覆蓋
file.write(update_content)
try:
with open(file_path, "r") as file:
file_content = file.read()
except Exception as e:
return str(e)
# 設定改寫內容
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]#獲取這行程式碼之前的內容
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就是修改後的完整檔案內容
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)
# 設定改寫內容
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]#獲取這行程式碼之前的內容
updated_content += "start_sentence_1和end_sentence_1之間的內容"
updated_content +=file_content[end_index_1:start_index_2]
updated_content += "start_sentence_2和end_sentence_2之間的內容"
updated_content +=file_content[end_index_2:start_index_3]
updated_content += "start_sentence_3和end_sentence_3之間的內容"
updated_content +=file_content[end_index_3:start_index_4]
updated_content += "start_sentence_4和end_sentence_4之間的內容"
updated_content += file_content[end_index_4:]
##此時updated_content就是修改後的完整檔案內容
if updated_content != "":
with open(file_path, "w") as file:
file.write(updated_content)
else:
print("修改失敗")
return -1