Issue
I am new with python and i have a txt including some defination such like this:
.subckt inv a z
mp z a vdd vdd pch
mn z a gnd gnd nch
.ends
.subckt pg in out clk clkb
mp in clkb out vdd pch
mn in clk out gnd nch
.ends
.subckt inv a z
mp z a vdd vdd pch
mn z a gnd gnd nch
.ends
Every section begin with .subckt
and end with .ends
And the first section and the third section are the same, how can i delete the duplicated defination?
Solution
Finally i got it! Really excited, add b
when the txt including any other content,list2
for using the original order
file=r'C:\Users\file.txt'
with open(file) as txt:
a=re.findall(r'\.subckt.*\.ends',txt.read(),re.M|re.S)
txt.seek(0)
b=re.sub(''.join(a),'',txt.read(),flags=re.S|re.M)
print(b.strip()+'\n')
list1=''.join(a).split('\n\n')
list2=list(set(list1))
list2.sort(key=list1.index)
for line in list2:
print(line+'\n')
Answered By - M_Sea