TypeError:无法使用Python3.5将“bytes”对象隐式转换为str

2024-09-29 17:45:24 发布

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

我在下面的代码中使用python3.5。在

def raxml(DIR,cleaned,num_cores,seqtype):
        assert cleaned.endswith(".aln-cln"),\
                "raxml infile "+cleaned+" not ends with .aln-cln"
        assert seqtype == "aa" or seqtype == "dna","Input data type: dna or aa"
        assert len(read_fasta_file(DIR+cleaned)) >= 4,\
                "less than 4 sequences in "+DIR+cleaned
        clusterID = cleaned.split(".")[0]
        tree = DIR+clusterID+".raxml.tre"
        raw_tree = "RAxML_bestTree."+cleaned
        model = "PROTCATWAG" if seqtype == "aa" else "GTRCAT"
        if not os.path.exists(tree) and not os.path.exists(raw_tree):
                # raxml crashes if input file starts with . 
                infasta = cleaned if DIR == "./" else DIR+cleaned
                cmd = ["raxml","-T",str(num_cores),"-p","12345","-s",\
                       infasta,"-n",cleaned,"-m",model]
                print (" ".join(cmd))
                p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
                out = p.communicate()
                assert p.returncode == 0,"Error raxml"+out[0]
        try:
                os.rename(raw_tree,tree)
                os.remove("RAxML_info."+cleaned)
                os.remove("RAxML_log."+cleaned)
                os.remove("RAxML_parsimonyTree."+cleaned)
                os.remove("RAxML_result."+cleaned)
                os.remove(DIR+cleaned+".reduced")
        except: pass # no need to worry about extra intermediate files
        return tree

它运行并返回以下代码:

^{pr2}$

最初,我尝试了以下方法:

 p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
 p = p.decode('utf-8')
 out = p.communicate()
 assert p.returncode == 0,"Error raxml"+out[0]

这根本没有解决问题。我看过类似的问题,但我不能想出一个解决办法。我希望能在这方面有所帮助。在

谢谢!在


Tags: cmdtreeifosdirnotassertout
2条回答

我不知道您的p.communicate()方法到底做了什么,但它似乎返回了一个byte对象。而这段代码,不能将这个byte对象添加到“Error raxml”str对象中:

assert p.returncode == 0,"Error raxml"+out[0]

也许你应该试着把它转换成str:

^{pr2}$

pPopen对象没有.decode(...)成员。在

你需要真正解码输出

p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out, _ = p.communicate()
out = out.decode('utf-8')
assert p.returncode == 0, 'Error raxml' + out[0]

也就是说,可以改进此代码以使用subprocess.check_output

^{pr2}$

或者你碰巧在用python3.6+

out = subprocess.check_output(cmd, encoding='UTF-8')

相关问题 更多 >

    热门问题