python remove()有可能会出现错误

  

import os, sys, pyperclip
import easygui as g
import re
file_name = 'tt3'
target_code_file = 'targetcode'
new_file = 'new_code2.text'
replace_code = r'''void test(void)
{
    int a=10;
    char s[10]={0};
    printf("a=%d,s=%s\n",a,s);
}
'''
pat = replace_code.split('\n')[0]
patt = '{:s}{:s}'.format(pat, '\n')
with open(target_code_file, 'r', encoding='utf-8') as trf:
    target_code_list = trf.readlines()
with open(file_name, 'r', encoding='utf-8') as rrf:
    old_lines = rrf.readlines()
target_code_list.append('\n')
target_code_list.reverse()
while patt in old_lines:
    tes = [i for i, x in enumerate(old_lines) if x == patt][0]
    for i in range(6):
        del (old_lines[tes])
    for i in target_code_list:
        old_lines.insert(tes, i)
old_lines = ''.join(old_lines)
print(old_lines)
with open(new_file, 'w', encoding='utf-8') as wf:
    wf.write(old_lines)

 

import os, sys, pyperclip
import easygui as g
import re
file_name = 'tt3'
target_code_file='targetcode'
new_file='new_code.text'
with open(file_name, 'r', encoding='utf-8') as rf:
    old_text = rf.read()

replace_code = r'''void test(void)
{
    int a=10;
    char s[10]={0};
    printf("a=%d,s=%s\n",a,s);
}
'''
target_code=r'''int func(int a){
    int fd=open("/dev/kd_camera_hw",O_RWD);
    if(fd < 0){
        printf("camera open fail\n");
        return -1;
    }else{
        printf("camera open success!\n");
        return 0;
    }
}
'''
# 把target变成列表

with open(target_code_file,'r',encoding='utf-8') as trf:
    target_code_list=trf.readlines()

pat = replace_code.split('\n')[0]
patt='{:s}{:s}'.format(pat,'\n')
# 找出文位置
pposi=0
posi=0
posil=0
str_posilist=[]
line_posilist=[]
# def find_posi(pposi):
#     posi=old_text.find(pat,pposi)
#     posil = old_text.count('\n', 0, posi)
#     return  posi,posil
# 把代码变成列表
with open(file_name, 'r', encoding='utf-8') as rrf:
    old_lines=rrf.readlines()
# while posi != -1:
# print(line_posilist)
# line-->[3, 17, 31, 45, 59]
# pposi -->[28, 215, 402, 588, 768,-1]

# posi, posil = find_posi(posi)
s_posilist=[]
posil_lis=[]
while   posi!=-1:
    posi=old_text.find(patt,pposi)

    posil = old_text.count('\n', 0, posi)
    pposi=posi+1
    s_posilist.append(posi)
    posil_lis.append(posil)
posil_lis.pop()
posil_lis.pop()
print(posil_lis)
for k in posil_lis:
    start=k+1

# 删除除void test(void)\n 以外的函数段
    for i in range(5):
        old_lines.remove(old_lines[start])
    # 插入target_code
    # 恢复到字符串
    old_text=''.join(old_lines)
    old_text=old_text.replace(patt,target_code,1)

    new_text=old_text
    print(new_text)


with open(new_file,'w',encoding='utf-8') as wf:
    wf.write(new_text)
    # over this

 

转载于:https://www.cnblogs.com/ezway/p/6747870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值