使用Python和xmlpath添加两个字符串的值

2024-09-27 23:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

它将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)相加。有人能给我个提示吗?我试过转换成float,但似乎不起作用

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

Tags: defaultevalnpxmlcglistdatfile
2条回答

您正在尝试将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))

在java中,可以添加字符串和浮点数。我的理解是,您需要添加值,然后显示它们。那就行了(把总数串起来)

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

相关问题 更多 >

    热门问题