繁体   English   中英

Python用变量写文件名

[英]Python writing filenames with variables

我正在为此代码中的三个参数的每次迭代和值创建 XML 文件:

def BOFunc():
fileCount = 1
q = 20
m=1
for child in root.iter('Traces'):
    child.find('TS')
    child.find('TW')
    for ts in frange(3,12.75,0.25):
        child.set('TS',str(ts))
        for tw in frange(3,12.75,0.25):
                child.set('TW',str(tw))
                for child2 in root.iter('Stackup'):
                    child2.find('core_h')
                    for h in frange(2,4,0.5):
                        for child3 in root.iter('Definition'):                        
                            child3.set('modelname', 'Output{}.xml'.format(fileCount))
                            child2.set('core_h',str(h))
                            tree.write('C:\Users\Aravind_Sampathkumar\Desktop\IMLC\BO\Output{}.xml'.format(fileCount))
                            D = threading.Thread(target=foo, args=(fileCount,))
                            D.start()
                            if m%5 == 0:
                                D.join()
                            fileCount += 1
                            q +=20
                            m +=1

一旦运行它创建了一个名为文件Output1.xmlOutput2.xml ,依此类推,直到该变量的最大值fileCount

有没有办法创建名称相同的 XML 文件:variable_name-Value?
例如: if TW = 3, TS = 4 and core_h = 2 ,文件名将是TW_3-TS_4-core_h_2.xml

您可以连接字符串中的变量值

filename = "TW_" + str(TW) + "-TS_" + str(TS) + "-core_h_" + str(h) + ".xml"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM