TypeError:需要类似字节的对象,而不是';str';从Python 2切换到Python 3时
我有一个在Python2中工作的代码,但不是Python3。该代码在Microsoft word文件中执行搜索和替换。我需要在python3中运行,但由于某些原因,代码无法运行。 这是我的密码TypeError:需要类似字节的对象,而不是';str';从Python 2切换到Python 3时,python,python-3.x,ms-word,Python,Python 3.x,Ms Word,我有一个在Python2中工作的代码,但不是Python3。该代码在Microsoft word文件中执行搜索和替换。我需要在python3中运行,但由于某些原因,代码无法运行。 这是我的密码 DOC = sys.argv[1] #doc = sys.argv[2] sample_name = str(DOC) sample_name = sample_name.split("X")[1] samp_name = sample_name.split("."
DOC = sys.argv[1]
#doc = sys.argv[2]
sample_name = str(DOC)
sample_name = sample_name.split("X")[1]
samp_name = sample_name.split(".")[0]
print (samp_name)
samp_cleaned = "Report {}".format(samp_name)
print (samp_cleaned)
out_file = "Report_{}.docx".format(samp_name)
print (out_file)
def create_doc(template_file, out_file, replaceText):
templateDocx = zipfile.ZipFile(template_file)
#outdir = '/'.join(out_file.split('/')[:-1])
#if not os.path.exists(outdir):
# os.makedirs(outdir)
newDocx = zipfile.ZipFile(out_file, "w")
for file in templateDocx.filelist:
content = templateDocx.read(file)
for key in replaceText.keys():
content = content.replace((key), str(replaceText[key]))
newDocx.writestr(file.filename, content)
templateDocx.close()
newDocx.close()
replaceText = {"0001":samp_name}
#out_file = basepath_out+template_file+SAMPLE
#out_file = "{}_Genome_report.docx".format(SAMPLE)
for key in replaceText.keys():
print (key)
out_file = out_file.replace(str(key), str(replaceText[key]))
create_doc(DOC, out_file,replaceText)
那么这就是我收到的错误
content = content.replace((key), str(replaceText[key]))
TypeError: a bytes-like object is required, not 'str'
如果您有任何想法,我们将不胜感激。您了解什么是
bytes
对象吗?您了解字节
和str
之间的区别吗?现在,您是否尝试检查程序中各种值的类型?