最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python替換文件中的指定行數(shù)技巧示例詳解

 更新時(shí)間:2023年09月07日 08:47:37   作者:cyl173  
這篇文章主要介紹了python替換文件中的指定行數(shù)技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1. 背景描述

最近在寫一個(gè)后端項(xiàng)目,主要的操作就是根據(jù)用戶的前端數(shù)據(jù),在后端打開項(xiàng)目中的代碼文件,修改對(duì)應(yīng)位置的參數(shù),因?yàn)樵谀壳暗暮蠖隧?xiàng)目中經(jīng)常使用這個(gè)操作,所以簡(jiǎn)單總結(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ù)進(jìn)行文件讀取,將數(shù)據(jù)存儲(chǔ)在緩沖區(qū)。

#1. 讀取文件
path='./test.c'
with open(path, 'r') as file:
 file_content = file.read()

查找文件替換位置

以查找chan_desc->ricean_factor = 1;//待修改位置為例,查找這句話的起點(diǎn)和終點(diǎn)。

## 注:此步驟需要import re
#2. 查找文件替換位置
start_index=file_content.find('chan_desc->ricean_factor  = ')#起點(diǎn)
end_index=file_content.find('chan_desc->aoa            = ',start_index)#終點(diǎn)
if end_index==-1 or start_index==-1:
 print('未找到待修改位置')
#此時(shí)得到的兩個(gè)指針,分別指向了待修改位置的起點(diǎn)和終點(diǎn),如下圖所示:

設(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-&gt;ricean_factor  = '+str(ricean_factor)+';//待修改位置'#修改這行代碼
update_content+=file_content[end_index:]#獲取這行代碼之后的內(nèi)容
#此時(shí)得到的update_content就是修改后的完整文件內(nèi)容,只修改了ricean_factor這一行的值

寫入文件

同樣使用python中的open函數(shù)。

#4. 寫入文件
if update_content!="":#如果修改內(nèi)容不為空
 with open(path, 'w') as file:#w表示覆蓋寫入,之前的內(nèi)容都會(huì)被覆蓋
     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-&gt;ricean_factor  = ')#起點(diǎn)
end_index=file_content.find('chan_desc-&gt;aoa            = ',start_index)#終點(diǎn)
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-&gt;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)容都會(huì)被覆蓋
     file.write(update_content)

3. 多行修改-操作步驟

多行修改思路
多行修改有兩種修改思路,如果修改部分比較集中,則可直接替換一整塊的字符串內(nèi)容,如果修改部分較為分散,則需要單獨(dú)查找修改位置,然后再分別進(jìn)行替換。

多行修改-整塊替換

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:]
 ##此時(shí)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:]
 ##此時(shí)updated_content就是修改后的完整文件內(nèi)容
 if updated_content != "":
  with open(file_path, "w") as file:
      file.write(updated_content)
else:
 print("修改失敗")
 return -1

以上就是python替換文件中的指定行數(shù)技巧示例詳解的詳細(xì)內(nèi)容,更多關(guān)于python替換文件指定行的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Django中Migrate和Makemigrations實(shí)操詳解

    Django中Migrate和Makemigrations實(shí)操詳解

    這篇文章主要為大家介紹了Django中Migrate和Makemigrations實(shí)操詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • python保存數(shù)據(jù)到本地文件的方法

    python保存數(shù)據(jù)到本地文件的方法

    今天小編就為大家分享一篇python保存數(shù)據(jù)到本地文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Django進(jìn)階之CSRF的解決

    Django進(jìn)階之CSRF的解決

    這篇文章主要介紹了Django進(jìn)階之CSRF的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • python?plotly設(shè)置go.Scatter為實(shí)線實(shí)例

    python?plotly設(shè)置go.Scatter為實(shí)線實(shí)例

    這篇文章主要為大家介紹了python?plotly設(shè)置go.Scatter為實(shí)線線條的樣式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Python使用read_csv讀數(shù)據(jù)遇到分隔符問題的2種解決方式

    Python使用read_csv讀數(shù)據(jù)遇到分隔符問題的2種解決方式

    read.csv()可以從帶分隔符的文本文件中導(dǎo)入數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Python使用read_csv讀數(shù)據(jù)遇到分隔符問題的2種解決方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • python SVM 線性分類模型的實(shí)現(xiàn)

    python SVM 線性分類模型的實(shí)現(xiàn)

    這篇文章主要介紹了python SVM 線性分類模型的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 在Python中操作文件之read()方法的使用教程

    在Python中操作文件之read()方法的使用教程

    這篇文章主要介紹了在Python中操作文件之read()方法的使用教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python+matplotlib實(shí)現(xiàn)量場(chǎng)圖的繪制

    Python+matplotlib實(shí)現(xiàn)量場(chǎng)圖的繪制

    matplotlib是基于Python語言的開源項(xiàng)目,pyplot提供一系列繪制2D圖形的方法。本文將帶大家學(xué)習(xí)matplotlib.pyplot.quiver()相關(guān)方法屬性并通過其繪制量場(chǎng)圖
    2021-12-12
  • Python實(shí)現(xiàn)中文大寫金額轉(zhuǎn)阿拉伯?dāng)?shù)字

    Python實(shí)現(xiàn)中文大寫金額轉(zhuǎn)阿拉伯?dāng)?shù)字

    在財(cái)務(wù)票據(jù)中,中文大寫金額被廣泛使用以防止篡改,但在數(shù)據(jù)處理時(shí),我們需要將其轉(zhuǎn)換為阿拉伯?dāng)?shù)字形式,下面我們就來看看如何使用Python實(shí)現(xiàn)這一轉(zhuǎn)換吧
    2025-08-08
  • python實(shí)現(xiàn)決策樹分類(2)

    python實(shí)現(xiàn)決策樹分類(2)

    這篇文章主要介紹了python實(shí)現(xiàn)決策樹分類的相關(guān)資料,用于實(shí)際的數(shù)據(jù)分類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評(píng)論

合水县| 清河县| 吐鲁番市| 新蔡县| 都安| 方正县| 汉沽区| 贵德县| 澳门| 灌南县| 嘉兴市| 陇西县| 呼玛县| 平远县| 焦作市| 静宁县| 社会| 武汉市| 阳东县| 辽中县| 溧水县| 高邮市| 什邡市| 忻城县| 托克逊县| 天镇县| 长治县| 彰武县| 宁安市| 南通市| 饶阳县| 镇巴县| 宜宾县| 沙坪坝区| 白银市| 海晏县| 文昌市| 厦门市| 吴桥县| 易门县| 兖州市|