当前位置: 首页 > news >正文

那曲市网站建设_网站建设公司_图标设计_seo优化

网站搭建服务,开个网络公司要多少钱,静态网页制作期末试卷及答案,网络公司网站官网【python技巧】替换文件中的某几行 1. 背景描述2. 单行修改-操作步骤3. 多行修改-操作步骤 1. 背景描述 最近在写一个后端项目#xff0c;主要的操作就是根据用户的前端数据#xff0c;在后端打开项目中的代码文件#xff0c;修改对应位置的参数#xff0c;因为在目前的后… 【python技巧】替换文件中的某几行 1. 背景描述2. 单行修改-操作步骤3. 多行修改-操作步骤 1. 背景描述 最近在写一个后端项目主要的操作就是根据用户的前端数据在后端打开项目中的代码文件修改对应位置的参数因为在目前的后端项目中经常使用这个操作所以简单总结一下。 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_flagschan_desc-free_flags|CHANMODEL_FREE_AMPS ;for (i 0; ichan_desc-nb_taps; i) {chan_desc-amps[i] pow(10,.1*epa_amps_dB[i]);sum_amps chan_desc-amps[i];}for (i 0; ichan_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()函数进行文件读取将数据存储在缓冲区。 #1. 读取文件 path./test.c with open(path, r) as file:file_content file.read()查找文件替换位置 以查找chan_desc-ricean_factor 1;//待修改位置为例查找这句话的起点和终点。 ## 注此步骤需要import re #2. 查找文件替换位置 start_indexfile_content.find(chan_desc-ricean_factor )#起点 end_indexfile_content.find(chan_desc-aoa ,start_index)#终点 if end_index-1 or start_index-1:print(未找到待修改位置) #此时得到的两个指针分别指向了待修改位置的起点和终点如下图所示设置替换文件内容 假设目前只修改这一行的参数 #3. 设置替换文件内容 ricean_factor3#假设这是要修改的参数信息 updata_contentfile_content[:start_index]#获取这行代码之前的内容 update_contentchan_desc-ricean_factor str(ricean_factor);//待修改位置#修改这行代码 update_contentfile_content[end_index:]#获取这行代码之后的内容 #此时得到的update_content就是修改后的完整文件内容只修改了ricean_factor这一行的值写入文件 同样使用python中的open函数。 #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_indexfile_content.find(chan_desc-ricean_factor )#起点 end_indexfile_content.find(chan_desc-aoa ,start_index)#终点 if end_index-1 or start_index-1:print(未找到待修改位置) #3. 设置替换文件内容 ricean_factor3#假设这是要修改的参数信息 updata_contentfile_content[:start_index]#获取这行代码之前的内容 update_contentchan_desc-ricean_factor str(ricean_factor);//待修改位置#修改这行代码 update_contentfile_content[end_index:]#获取这行代码之后的内容 #4. 写入文件 if update_content!:#如果修改内容不为空with open(path, w) as file:#w表示覆盖写入之前的内容都会被覆盖file.write(update_content)3. 多行修改-操作步骤 多行修改思路 多行修改有两种修改思路如果修改部分比较集中则可直接替换一整块的字符串内容如果修改部分较为分散则需要单独查找修改位置然后再分别进行替换。多行修改-整块替换 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;\nupdated_content start_sentence和end_sentence之间的sentence_2;\nupdated_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 -1or end_index_1 -1or start_index_2 -1or end_index_2 -1or start_index_3 -1or end_index_3 -1or start_index_4 -1or 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
http://www.lebaoying.cn/news/72762.html

相关文章:

  • 书店网站的建设wordpress微视频主题
  • 2017网站主流设计风格网上有做口译的网站么
  • 莆田网站建设开发wordpress 网站卡
  • 苏州营销网站设计word发布wordpress
  • 梅州企业网站有什么可以做翻译的网站吗
  • 石家庄站全景图公司网站建设服务费计入什么科目
  • 杭州盘石做网站专业吗网站制作网站建设
  • 二手车东莞网站建设国家建设部门三类人员官方网站
  • 深圳市网站建设公司设计公司学院 网站 两学一做
  • 阿里巴巴外贸网站论坛dede部署两个网站
  • 单页面网站 万网x3网站搭建流程负责人
  • 深圳商城网站开发做网站素材在哪找
  • 某物流网站后台源码亿码酷网站建设
  • 明星网站建设重庆市建设安全监督站的网站
  • 单位网站的方案月夜直播在线看免费
  • 番禺网站开发万网官网登录
  • 上海网站制作哪家奿wordpress前端调用插件函数
  • 做推广能提高网站权重么wordpress怎么设置404伪静态
  • 安徽省建设监理协会新网站南阳建设工程信息网站
  • 济南网站建设联 系小七wordpress qiniu
  • 简单建站网站转app工具
  • 网站建设的缺点做网站程序的都不关注seo
  • 电脑网站适应手机如何做企业培训课程设计
  • 网站设计制作 厂建设工程学部研究生培养网站
  • 仪征市城乡建设局网站电子请柬免费制作软件app
  • 济南网站制作运营网站建设实训个人总结1000字
  • 静安青岛网站建设wordpress wp_video_shortcode
  • 杭州外贸网站建设企业网站建设协议
  • 微信公众号开发网站建设好看的网站颜色
  • 网站建设与部署阿里云大学济南网站系统优化