python用变量编写文件名

时间:2018-05-14 11:07:43

标签: python xml

我正在为每次迭代创建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.xml,Output2.xml的文件,依此类推,直到变量fileCount的最大值。

有没有办法可以创建名称相同的xml文件:variable_name-Value? 例如: 如果TW = 3,TS = 4且core_h = 2,则文件名为TW_3-TS_4-core_h_2.xml

感谢任何帮助或指示!!

1 个答案:

答案 0 :(得分:2)

您可以在字符串

中连接变量值
filename = "TW_" + str(TW) + "-TS_" + str(TS) + "-core_h_" + str(h) + ".xml"