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
时间: 2024-07-30 10:17:59

python remove()有可能会出现错误的相关文章

Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level

[问题] 一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: IndentationError: unindent does not match any outer indentation level [解决过程] 1.对于此错误,最常见的原因是,的确没有对齐.但是我根据错误提示的行数,去代码中看了下,没啥问题啊. 都是用TAB键,对齐好了的,没有不对齐的行数啊. 2.以为是前面的注释的内容影响后面的语句的语法了,所以把前面的注释

Python读取中文txt文件错误:UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character

1 with open(file,'r') as f: 2 line=f.readline() 3 i=1 4 while line: 5 line=line.decode('utf-8') 6 print str(i)+": "+line7 line=f.readline() 8 i=i+1 用以上代码读取一个包含中文的txt文件时,在正确地读取并打印了六百多行之后,print str(i)+": "+line这一行报错: UnicodeEncodeError:

【python】socket编程常量错误问题-1 &#39;AF_INET&#39;错误

1 2 3 4 5 6 7 8 9 # -*- coding: utf-8 -*- import socket print "Creating socket" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  print "Done!" print "Connecting to remote host..." s.connect(("www.baidu.com", 8

Error:cannot import name&#39;is_list_like&#39; --python使用pandas_datareader时出现错误,解决方案

1 import pandas as pd 2 pd.core.common.is_list_like = pd.api.types.is_list_like 在下行代码之前插入 from pandas_datareader import data 这个方法不必修改本地文件,在pandas_datareader 0.7.0版本会修复这个问题. Error:cannot import name'is_list_like' --python使用pandas_datareader时出现错误,解决方案

python学习三十天(错误处理)

今日内容: 1.昨日作业 2.错误处理 一,昨日作业 题目:三级菜单使用递归和栈的方式写 递归方式 字典: university_list = { "安徽省": { "合肥": ["中国科技大学", "合肥工业大学"], "安庆": ["安庆学院"], "阜阳": ["阜阳学院"] }, "江苏省": { "南京&qu

python中常见的那些错误及解决方法(不定更新)

错误1:SyntaxError: 'return' outside function解决:将return放在方法体中return不能在方法以外使用 错误2:TypeError: must be str, not int类型错误 必须是一个字符串 不能是数字解决办法:使用+拼接的时候 必须使用字符串,或者将数字转化成字符串 错误3:SyntaxError: invalid syntax语法错误 非法的语法解决办法:看报错信息在第几行 ,从这一行往上找错误 错误4:IndentationError:

Ubuntu中python环境下import requests错误的解决(学习过程问题记录)

python中导入模块:import requests提示错误,错误信息描述如下: >>>import requests Traceback (most recent last):   File "<stdin>", line 1, in <module> ImportError:No module named requests 意思就是说没有名为requests的模块,解决方案是在命令行下使用 pip install requests 来进行

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1, 

Python初学的易犯错误

当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在类似如下代码中: ? 1 2 if spam == 42     print('Hello!') 2)使用 = 而不是 ==(导致“SyntaxError: i