类型错误:Can';t转换为';字节';对象使用Python3.5隐式地创建str

类型错误:Can';t转换为';字节';对象使用Python3.5隐式地创建str,python,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

我在下面的代码中使用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
它运行并返回以下代码:

"raxml_wrapper.py", line 30, in raxml
        assert p.returncode == 0,"Error raxml"+out[0]
TypeError: Can't convert 'bytes' object to str implicitly
最初,我尝试了以下方法:

 p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
 p = p.decode('utf-8')
 out = p.communicate()
 assert p.returncode == 0,"Error raxml"+out[0]
这根本无法解决问题。我已经研究过类似的问题,但我无法找到解决办法。我希望能在这方面得到一些帮助


谢谢

我不知道你的p.communicate()方法到底做什么,但它似乎返回了一个byte对象作为结果。这段代码无法将此字节对象添加到“Error raxml”str对象:

assert p.returncode == 0,"Error raxml"+out[0]
也许您应该尝试将其转换为str,如下所示:

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

p
,一个
Popen
对象,没有
.decode(…)
成员

您需要实际解码输出

也就是说,可以改进此代码以使用
子流程。请检查\u输出

# does roughly the same thing, you'll get `subprocess.CalledProcessError` instead of `AssertionError`
out = subprocess.check_output(cmd).decode('UTF-8')
或者如果您碰巧正在使用python3.6+

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

我假设Python3?尝试
binascii
导入binascii
然后
打印(binascii.hexlify(这里是bytes\u).decode())
@Mick\ubinascii是否将字节转换为字符串binascii可以用数据将字节表示为十六进制值字符串。除非您提供一些关于如何使用数据的信息,否则很难就更好的解决方案提出建议。Hello@Anthony我这次仍然收到一条断言错误消息:AssertionError:error Raxml这是我迄今为止添加的内容,以找到解决方案:out,=p.communicate()out=subprocess。check_output(cmd)。decode('UTF-8')print(out)assert p.returncode==0,“Error raxml”+out[0]啊,你需要分别使用我的解决方案,这三个代码块是做大致相同事情的选项(不要同时使用它们!)我的错误@Anthony我只是按原样使用了第一个块。我仍然得到断言错误:error raxml。可能意味着您的命令正在退出非零状态——当您单独在终端中运行它时会发生什么?(可能在运行后检查
echo$?
out = subprocess.check_output(cmd, encoding='UTF-8')