我有一个可以包含其他文件的文件。我需要打开包含文件并确定特定文件的文件扩展名(demo.spx)。例如:
文件名:sample.txt
* SetUp Time Simulation
*****************************************************
*Options
.options nomod
*+ autostop=0
*+ rmax=2
*+ absv=1E-6
*+ relv=1E-3
*+ trtol=0.1
*+ lvltim=3
*+ dvdt=2
*+ relvar=0.2
*+ absvar=0.2
*+ ft=0.2
*+ relmos=0.01
*+ method=TRAP
*+ notop=0
+ post=1
+ runlvl=5 rmax=25
+ ingold=2
+ CO=132
+ MEASFORM = 3
.WIDTH OUT=132
.include './test.sp'
文件:test.sp
*****************************************************
* Circuit definition
.include **'demo.spx'**
.param vdd=0.99
.param vss=0
.temp=-40C
*Supplies
.global VDD VSS VBP VBN
Vdd VDD 0 vdd
Vss VSS 0 vss
Vbp VBP 0 vdd
Vbn VBN 0 0
Vsi SI 0 0
Vse SE 0 0
我编写了以下代码,但看起来我做错了。所以首先我检查了我已经格式化,如果进入第一个文件,发现返回。如果我没有,我会查看包含的文件并尝试查看第二个文件。我们需要递归搜索两次。 第二版代码
我再次修改了代码并能够确定文件扩展名 第一级但第二级我无法确定文件扩展名 谁回报我没有。评论也欢迎,如果我可以提高我的 代码
#!/usr/bin/env py
import os
import sys
def parse_file_extension(gold_deck, found, count):
extention_list = [ "lvs", "cir", "spx"]
if( count == 2 or found == True):
return
with open(gold_deck, 'r+') as fspi:
while 1:
data = fspi.readline()
if not data:
break
if data.startswith('.include'):
data = data.split()
print data
netlist_file_extension = data[1].split(".")[-1].rstrip("'")
print netlist_file_extension
if netlist_file_extension in extention_list:
netlist_file = os.path.basename(data[1]).strip("'")
count = count + 1
found = True
print "First include"
print count
return netlist_file
else:
gold_deck = os.path.basename(data[1]).rstrip("'")
print gold_deck
parse_file_extension(gold_deck, found, count)
def main(argv):
gold_deck = "sample.txt"
netlist_file = parse_file_extension(gold_deck, False, 0)
print netlist_file **//None Expecting demo.spx**
if __name__ == "__main__":
sys.exit(main(sys.argv))
答案 0 :(得分:1)
我试图根据这些考虑重构你的工作:
然后我对你上一次代码解决方案的评论很少:
我希望这可以提供帮助。
#!/usr/bin/env py
from os.path import splitext
import sys
def parse_file_extension(gold_deck, depth):
extention_list = [ "lvs", "cir", "spx"]
fspi = open(gold_deck, 'r+')
for data in fspi:
if data.startswith('.include'):
data = data.split()
netlist_file,netlist_file_extension = splitext(data[1].strip("'"))
if netlist_file_extension not in extention_list:
netlist_file = parse_file_extension(netlist_file, depth+1)
return netlist_file
fspi.close()
def main(argv):
gold_deck = "sample.txt"
netlist_file = parse_file_extension(gold_deck, 0)
print netlist_file
if __name__ == "__main__":
sys.exit(main(sys.argv))
答案 1 :(得分:0)
替换
return netlist_file
与
yield netlist_file
所以你的函数返回迭代器。
files=[parse_file_extension(gold_deck, found, count)]
将生成文件列表。
答案 2 :(得分:0)
在我的分析之后,我已经将这个问题修复为一级和二级搜索,并使用以下代码实现。如果有人在下面的代码中提供代码通信/优化,那将会很有帮助。
#!/usr/bin/env py
import os
import sys
def parse_file_extension(gold_deck, found, count):
extention_list = [ "lvs", "cir", "spx"]
with open(gold_deck, 'r+') as fspi:
while 1:
data = fspi.readline()
if not data:
break
if data.startswith('.include'):
data = data.split()
netlist_file_extension = data[1].split(".")[-1].strip("'")
count = count + 1
if netlist_file_extension in extention_list:
found = True
netlist_file = os.path.basename(data[1]).strip("'")
return netlist_file
else:
gold_deck = os.path.basename(data[1]).strip("'")
netlist_file = parse_file_extension(gold_deck, found, count)
return netlist_file
def main(argv):
gold_deck = "sample.txt"
#gold_deck = "test.sp"
netlist_file = parse_file_extension(gold_deck, False, 0)
print netlist_file
if __name__ == "__main__":
sys.exit(main(sys.argv))