Python 在一个文件的两行之间复制代码,并在另一个文件的两行之间覆盖代码
我试图从一个文件中复制两个特定行之间的代码,并将其粘贴到另一个文件中相同的两个对应行之间 例如,我有两个文件test.sv_old和test.sv。我想在下面两行之间将test.sv_旧文件中的代码复制到test.sv 第1行:“//此处开始功能规范” 第2行://如果没有供应,则输出设置为0。根据需要取消注释 以下是test.sv_旧文件的内容:Python 在一个文件的两行之间复制代码,并在另一个文件的两行之间覆盖代码,python,python-3.x,Python,Python 3.x,我试图从一个文件中复制两个特定行之间的代码,并将其粘贴到另一个文件中相同的两个对应行之间 例如,我有两个文件test.sv_old和test.sv。我想在下面两行之间将test.sv_旧文件中的代码复制到test.sv 第1行:“//此处开始功能规范” 第2行://如果没有供应,则输出设置为0。根据需要取消注释 以下是test.sv_旧文件的内容: `include "def.sv" /PRIMARY /SECONDARY /TERTIARY /UNASSIGNED module a
`include "def.sv"
/PRIMARY
/SECONDARY
/TERTIARY
/UNASSIGNED
module abc ( );
we want to see this too
//Start of functional specification here
//Functional cell instantiation
abc_real Inst0 (.z1(int_z1),
.z2(int_z2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // abc
`include "def.sv"
/PRIMARY
/SECONDARY
/TERTIARY
/UNASSIGNED
module xyz ( );
//Start of functional specification here
//Functional cell instantiation
xyz_real Inst0 (.y1(int_y1),
.y2(int_y2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // xyz
`include "def.sv"
/PRIMARY
/SECONDARY
/TERTIARY
/UNASSIGNED
module lmn ( );
//Start of functional specification here
//Functional cell instantiation
lmn_real Inst0 (.x1(int_x1),
.x2(int_x2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // lmn
这是我的test.sv文件:
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module abc ( );
keep this code untouched
no change needed here
//Start of functional specification here
//Functional cell instantiation
some garbage
here
just replace this
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // abc
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module xyz ( );
keep this as it is
input a1;
//Start of functional specification here
//Functional cell instantiation
some garbage
here and there
why not just replace this
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // xyz
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module lmn ( );
keep this as it is
input a1;
//Start of functional specification here
//Functional cell instantiation
some garbage
here and there
why not just replace this
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // lmn
我尝试了以下代码,但它没有提供我所需的确切输出:
import sys,re,os
rf_SVFile=open(sys.argv[1],"r")
wtstring = ""
wtindex = 0
copy = False
write = False
print("Copying instantiation code from {} to new SV file {}".format(rf_SVFile.name,sys.argv[2]))
for vline in rf_SVFile:
if vline.strip() == "//Start of functional specification here" and copy == False:
copy = True
elif vline.strip() == "// Outputs set to 0 if no supply. Uncomment as needed.":
copy = False
elif copy:
wtstring = wtstring + vline # wtstring has the functional code between two lines which you want to write to .sv file
with open(sys.argv[2], "r+") as wf_NewSVFile:
insert = False
contents = wf_NewSVFile.readlines()
for index, svline in enumerate(contents):
if svline.strip() == "// Outputs set to 0 if no supply. Uncomment as needed.":
wtindex = index
insert = True
break
contents.insert(wtindex,wtstring) # contents has complete code in list format, instantantiation code is copied from SV file to new SV File
stringContents = "".join(contents) # convert list into string in order to write it to .sv file
if insert:
wf_NewSVFile.seek(0, 0)
wf_NewSVFile.write(str(stringContents))
else:
print(
'Warning: No "/ Outputs set to 0 if no supply. Uncomment as needed." line found in {}, hence code is not being copied to new SV file',NewSVFile)
下面是由上述代码生成的修改后的test.sv文件:
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module abc ( );
keep this code untouched
no change needed here
//Start of functional specification here
//Functional cell instantiation
some garbage
here
just replace this
//Functional cell instantiation
abc_real Inst0 (.z1(int_z1),
.z2(int_z2),
.a1(reg_a1));
//Functional cell instantiation
xyz_real Inst0 (.y1(int_y1),
.y2(int_y2),
.a1(reg_a1));
//Functional cell instantiation
lmn_real Inst0 (.x1(int_x1),
.x2(int_x2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // abc
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module xyz ( );
keep this as it is
input a1;
//Start of functional specification here
//Functional cell instantiation
some garbage
here and there
why not just replace this
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // xyz
`include "def.sv"
//PRIMARY
//SECONDARY
//TERTIARY
//UNASSIGNED
module lmn ( );
keep this as it is
input a1;
//Start of functional specification here
//Functional cell instantiation
some garbage
here and there
why not just replace this
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // lmn
谁能解释一下,我做错了什么?谢谢
问题:谁能解释一下,我做错了什么
您的代码远未完成。我建议采用以下逻辑:
test.sv_old
和test.sv
文件模块
到endmodule
制作一个列表
的目录{:}
在这些条件下:
From
test.sv_old
仅读取From//Functional
直到
//输出
从
test.sv
读取从//Functional
到//Output
的所有,将//Functional
保留为占位符test.sv
dict{:}
逐行写入//Functional
行中,从test.sv\u old
切换到dict
,然后编写整个
test.sv
这似乎是你想要的。i、 e.当i
diff
您的所需的test.sv到test\u so79.new.txt时,它们是相等的。我留下了一些调试内容,您可能需要;-)正则表达式的拆分来自
为了详细说明我对zip
lockstep约束的评论,如果旧的模块序列是abc,xyz
,而新的模块序列是xyz,abc
,那么您必须以稍微不同的方式对待事情
这应该让你开始:
di_old_module = dict([(module.name,module) for module in modules_old])
....
for mn in modules_new:
mo = di_old_module[mn.name]
_, keep, _ = chunker_module(mo)
p1, _, p3 = chunker_module(mn)
使用一些关键字创建索引,然后加入切片应该可以做到这一点
with open('test.sv') as f:
content = f.readlines()
with open('test_old.sv') as f:
content_2 = f.readlines()
cp_s = [i for i, v in enumerate(content_2) if 'Functional' in v]
cp_end = [i for i, v in enumerate(content_2) if 'Outputs' in v]
dest_s = [i for i, v in enumerate(content) if 'Functional' in v]
dest_end = [i for i, v in enumerate(content) if 'Outputs' in v]
new = content[:dest_s[0]] + content_2[cp_s[0]: cp_end[0]] + content[dest_end[0]: dest_s[1]] +
content_2[cp_s[1]:]
with open('fixed.sv', 'w') as f:
f.write(''.join(new))
输出:
read()(不是realine(),两个文件,string.split()如何在两个标记注释上,分别给你3个块。文件输出将变成f2p1、f1p2、F2P3停止。停止它。你显然是在尝试实现源代码管理用于生成和应用补丁的功能。寻找现有的工具并使用它们。或者,如果你的问题更典型,你可能可以使用一个你可以使用的模板根据一些配置参数展开。不管这是什么,它都是。谢谢你的回答。这非常有效。我也喜欢你将两个文件分成3部分,然后选择f2p1、f1p2、f2p3的逻辑。你能详细介绍一下使用zip函数的for-loop吗?你需要阅读zip。但基本上它假设你的两个块流是一个如果它们开始发散,即旧[n]与新[n]不匹配,那么这将不起作用。好的,谢谢。另外,请解释一下“assert len(res)==5”这一行,为什么要比较patre.split(module_)到5?你需要确保你的正则表达式拆分按预期进行。我发现的大部分复杂性是将正则表达式拆分重新缝合在一起,你需要对它们所做的事情保持关注。我最初认为此拆分将返回3项。查看它,我看到了5项,并看到我可以影响所需的行为by 1+2,3,4+5。但是,如果有4或6个项目,那么我必须调整代码,以便
断言
在不是5的情况下抛出异常。奇怪的是,它会断开并且len(res)当我添加一些额外的行时,仅为1。我尝试使用模块模式,但无法使其工作。您能最后一次在我的原始帖子中使用test.sv和test.sv_old进行尝试吗?这非常棒,非常简单,工作起来很有魅力。@sanforyou很乐意帮助:)这实际上是非常有趣的切片让我注意到我怎样才能使它更通用。。如果我有两个以上的模块?如果我添加了第三个模块,只是复制了第三个模块中的所有内容,它就会崩溃。@sanforyou这涉及到对两个文件进行切片并合并它们,如果您有第三个文件,则必须相应地将其与您想要添加该内容的任何文件进行切片。不,我只有两个文件,但当我添加第三个模块(模块lmn)时那就不行了。我已经用新的test.sv_旧文件更新了我的主要帖子。
with open('test.sv') as f:
content = f.readlines()
with open('test_old.sv') as f:
content_2 = f.readlines()
cp_s = [i for i, v in enumerate(content_2) if 'Functional' in v]
cp_end = [i for i, v in enumerate(content_2) if 'Outputs' in v]
dest_s = [i for i, v in enumerate(content) if 'Functional' in v]
dest_end = [i for i, v in enumerate(content) if 'Outputs' in v]
new = content[:dest_s[0]] + content_2[cp_s[0]: cp_end[0]] + content[dest_end[0]: dest_s[1]] +
content_2[cp_s[1]:]
with open('fixed.sv', 'w') as f:
f.write(''.join(new))
chrx@chrx:~/python/stackoverflow/10.11$ cat fixed.sv
module abc ( );
keep this code untouched
no change needed here
//Start of functional specification here
//Functional cell instantiation
abc_real Inst0 (.z1(int_z1),
.z2(int_z2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // abc
module xyz ( );
keep this as it is
input a1;
//Start of functional specification here
//Functional cell instantiation
xyz_real Inst0 (.z1(int_z1),
.z2(int_z2),
.a1(reg_a1));
// Outputs set to 0 if no supply. Uncomment as needed.
endmodule // xyz