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

python替換文件中的某幾行操作技巧

 更新時(shí)間:2023年09月07日 08:53:21   作者:CrazyPixel  
這篇文章主要介紹了python替換文件中的某幾行,本文介紹使用python正則庫(kù)打開(kāi)文件并替換文件中某幾行數(shù)據(jù)的可行方法,需要的朋友可以參考下

python替換文件中的某幾行操作技巧

本文介紹使用python正則庫(kù)打開(kāi)文件并替換文件中某幾行數(shù)據(jù)的可行方法。

【python技巧】替換文件中的某幾行

1. 背景描述

最近在寫(xiě)一個(gè)后端項(xiàng)目,主要的操作就是根據(jù)用戶(hù)的前端數(shù)據(jù),在后端打開(kāi)項(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. 單行修改-操作步驟

1.讀取文件

使用python中的open()函數(shù)進(jìn)行文件讀取,將數(shù)據(jù)存儲(chǔ)在緩沖區(qū)。

path='./test.c'
with open(path, 'r') as file:
    file_content = file.read()

2.查找文件替換位置

以查找chan_desc->ricean_factor = 1;//待修改位置為例,查找這句話(huà)的起點(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('未找到待修改位置')

3.設(shè)置替換文件內(nèi)容

假設(shè)目前只修改這一行的參數(shù),

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)容
#此時(shí)得到的update_content就是修改后的完整文件內(nèi)容,只修改了ricean_factor這一行的值

4.寫(xiě)入文件

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

if update_content!="":#如果修改內(nèi)容不為空
    with open(path, 'w') as file:#w表示覆蓋寫(xiě)入,之前的內(nèi)容都會(huì)被覆蓋
        file.write(update_content)

5.總代碼

整體的代碼如下所示:

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  = ')#起點(diǎn)
end_index=file_content.find('chan_desc->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->ricean_factor  = '+str(ricean_factor)+';//待修改位置'#修改這行代碼
update_content+=file_content[end_index:]#獲取這行代碼之后的內(nèi)容
#4. 寫(xiě)入文件
if update_content!="":#如果修改內(nèi)容不為空
    with open(path, 'w') as file:#w表示覆蓋寫(xiě)入,之前的內(nèi)容都會(huì)被覆蓋
        file.write(update_content)

3. 多行修改-操作步驟

1.多行修改思路

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

2.多行修改-整塊替換

try:
    with open(file_path, "r") as file:
            file_content = file.read()
except Exception as e:
    return str(e)
# 設(shè)置改寫(xiě)內(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

3.多行修改-局部替換

try:
    with open(file_path, "r") as file:
            file_content = file.read()
except Exception as e:
    return str(e)
# 設(shè)置改寫(xiě)內(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

到此這篇關(guān)于python替換文件中的某幾行的文章就介紹到這了,更多相關(guān)python替換文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python通過(guò)kerberos安全認(rèn)證操作kafka方式

    Python通過(guò)kerberos安全認(rèn)證操作kafka方式

    這篇文章主要介紹了Python通過(guò)kerberos安全認(rèn)證操作kafka方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • 深度學(xué)習(xí)小工程練習(xí)之tensorflow垃圾分類(lèi)詳解

    深度學(xué)習(xí)小工程練習(xí)之tensorflow垃圾分類(lèi)詳解

    這篇文章主要介紹了練習(xí)深度學(xué)習(xí)的一個(gè)小工程,代碼簡(jiǎn)單明確,用來(lái)作為學(xué)習(xí)深度學(xué)習(xí)的練習(xí)很適合,對(duì)于有需要的朋友可以參考下,希望大家可以體驗(yàn)到深度學(xué)習(xí)帶來(lái)的收獲
    2021-04-04
  • pandas 如何分割字符的實(shí)現(xiàn)方法

    pandas 如何分割字符的實(shí)現(xiàn)方法

    這篇文章主要介紹了pandas 如何分割字符的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 五個(gè)有趣的Python整蠱小程序合集

    五個(gè)有趣的Python整蠱小程序合集

    Python 能做很多無(wú)聊,但有意思的事情。本文為大家精心準(zhǔn)備了五個(gè)整蠱朋友的小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴快動(dòng)手試一試
    2022-04-04
  • Python+Opencv身份證號(hào)碼區(qū)域提取及識(shí)別實(shí)現(xiàn)

    Python+Opencv身份證號(hào)碼區(qū)域提取及識(shí)別實(shí)現(xiàn)

    這篇文章主要介紹了Python+Opencv身份證號(hào)碼區(qū)域提取及識(shí)別實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • python繪制直方圖和密度圖的實(shí)例

    python繪制直方圖和密度圖的實(shí)例

    今天小編就為大家分享一篇python繪制直方圖和密度圖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python zip文件 壓縮

    python zip文件 壓縮

    看了我前面的一系列文章,不知道你會(huì)不會(huì)覺(jué)得python是無(wú)所不能的,我現(xiàn)在就這感覺(jué)!如何用python進(jìn)行文件壓縮呢
    2008-12-12
  • Python的Flask框架中實(shí)現(xiàn)登錄用戶(hù)的個(gè)人資料和頭像的教程

    Python的Flask框架中實(shí)現(xiàn)登錄用戶(hù)的個(gè)人資料和頭像的教程

    這篇文章主要介紹了Python的Flask框架中實(shí)現(xiàn)登錄用戶(hù)的個(gè)人資料和頭像的教程,這也是各個(gè)web框架的最基本功能之一,需要的朋友可以參考下
    2015-04-04
  • python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì)

    python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì)

    這篇文章主要介紹了python反轉(zhuǎn)(逆序)字符串的6種方法詳細(xì),需要的朋友可以參考下
    2021-04-04
  • Python直接賦值與淺拷貝和深拷貝實(shí)例講解使用

    Python直接賦值與淺拷貝和深拷貝實(shí)例講解使用

    淺拷貝,指的是重新分配一塊內(nèi)存,創(chuàng)建一個(gè)新的對(duì)象,但里面的元素是原對(duì)象中各個(gè)子對(duì)象的引用。深拷貝,是指重新分配一塊內(nèi)存,創(chuàng)建一個(gè)新的對(duì)象,并且將原對(duì)象中的元素,以遞歸的方式,通過(guò)創(chuàng)建新的子對(duì)象拷貝到新對(duì)象中。因此,新對(duì)象和原對(duì)象沒(méi)有任何關(guān)聯(lián)
    2022-11-11

最新評(píng)論

定结县| 治多县| 古田县| 阜康市| 麦盖提县| 凭祥市| 交口县| 万盛区| 玉溪市| 巫山县| 昆明市| 锡林郭勒盟| 北海市| 中西区| 赞皇县| 图们市| 郎溪县| 凤翔县| 轮台县| 丰城市| 阿拉善盟| 徐水县| 临泉县| 仙游县| 竹北市| 那坡县| 高淳县| 开化县| 大姚县| 龙口市| 临高县| 南丹县| 沛县| 岢岚县| 武汉市| 日喀则市| 安顺市| 湘潭市| 武定县| 泰顺县| 永泰县|