使用Python和XML路径添加两个字符串的值

时间:2016-08-02 14:00:31

标签: python xml xml-parsing

它使用wallTime和setupwalltime生成一个输出到dat文件中,该文件具有以下格式:

24000   4       0
81000   17      0
192000  59      0
648000  250     0
1536000 807     0
3000000 2144    0
6591000 5699    0

我想知道如何将两个值(即wallTime和setupwalltime)一起添加。有人能给我一个暗示吗?我尝试转换为浮动,但它似乎不起作用。

import libxml2
import os.path
from numpy import *
from cfs_utils import *

np=[1,2,3,4,5,6,7,8]
n=[20,30,40,60,80,100,130]
solver=["BiCGSTABL_iluk", "BiCGSTABL_saamg", "BiCGSTABL_ssor" ,  "CG_iluk", "CG_saamg", "CG_ssor" ]# ,"cholmod", "ilu" ]
file_list=["eval_BiCGSTABL_iluk_default",  "eval_BiCGSTABL_saamg_default" , "eval_BiCGSTABL_ssor_default" ,  "eval_CG_iluk_default","eval_CG_saamg_default", "eval_CG_ssor_default" ] # "simp_cholmod_solver_3D_evaluate", "simp_ilu_solver_3D_evaluate" ]

for cnt_np in np:
    i=0
    for sol in solver:
            #open write_file= "Graphs/" +  "Np"+ cnt_np + "/CG_iluk.dat"
            #"Graphs/Np1/CG_iluk.dat"


            write_file = open("Graphs/"+ "Np"+ str(cnt_np) + "/" + sol + ".dat", "w")
            print("Reading " + "Graphs/"+ "Np"+ str(cnt_np) + "/" + sol + ".dat"+ "\n")

            #loop through different unknowns
            for cnt_n in n:

                    #open file "cfs_calculations_" + cnt_n +"np"+ cnt_np+ "/" +  file_list(i) + "_default.info.xml"

                    read_file = "cfs_calculations_" +str(cnt_n) +"np"+ str(cnt_np) + "/" +  file_list[i] + ".info.xml"
                    print("File list" + file_list[i] + "vlaue of  i  " + str(i) + "\n")
                    print("Reading " + " cfs_calculations_" +str(cnt_n) +"np"+ str(cnt_np) + "/" +  file_list[i] + ".info.xml" )
                    #read wall and cpu time and write

                    if os.path.exists(read_file):
                            doc = libxml2.parseFile(read_file)
                            xml = doc.xpathNewContext()

                            walltime = xpath(xml, "//cfsInfo/sequenceStep/OLAS/mechanic/solver/summary/solve/timer/@wall")
                            setupwalltime = xpath(xml, "//cfsInfo/sequenceStep/OLAS/mechanic/solver/summary/setup/timer/@wall")
                    #       cputime = xpath(xml, "//cfsInfo/sequenceStep/OLAS/mechanic/solver/summary/solve/timer/@cpu")
                    #       setupcputime = xpath(xml, "//cfsInfo/sequenceStep/OLAS/mechanic/solver/summary/solve/timer/@cpu")
                            unknowns = 3*cnt_n*cnt_n*cnt_n
                            write_file.write(str(unknowns) + "\t" + walltime + "\t" + setupwalltime + "\n")
                            print("Writing_point" + str(unknowns) + "%f" ,float(setupwalltime  ) )
                            doc.freeDoc()
                            xml.xpathFreeContext()
            write_file.close()
            i=i+1

2 个答案:

答案 0 :(得分:0)

在java中,您可以添加字符串和浮点数。我的理解是你需要添加值然后显示它们。这将起作用(串联总和)

write_file.write(str(unknowns) + "\f" + str(float(walltime) + float(setupwalltime)) + "\n") 

答案 1 :(得分:0)

您正尝试将str添加到float。这不起作用。如果要使用字符串连接,请先将所有值强制转换为str。试试这个:

write_file.write(str(unknowns) + "\t" + str(float(walltime) + float(setupwalltime)) + "\n")

或者,或许更可读:

totalwalltime = float(walltime) + float(setupwalltime)
write_file.write("{}\t{}\n".format(unknowns, totalwalltime))