Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:需要类似字节的对象,而不是';str';蟒蛇3_Python_Python 3.x - Fatal编程技术网

Python TypeError:需要类似字节的对象,而不是';str';蟒蛇3

Python TypeError:需要类似字节的对象,而不是';str';蟒蛇3,python,python-3.x,Python,Python 3.x,在我的项目中,我将动态地将样式写入HTML文件。我最近从python2迁移到了3。现在它抛出一个错误,如下所示: 代码段: html_text = markdown(html_content, output_format='html4') css = const.URL_SCHEME + "://" + request_host + '/static/css/pdf.css' css = css.replace('\\', "/") # HTML File Output

在我的项目中,我将动态地将样式写入HTML文件。我最近从python2迁移到了3。现在它抛出一个错误,如下所示:

代码段:

 html_text = markdown(html_content, output_format='html4')
    css = const.URL_SCHEME + "://" + request_host + '/static/css/pdf.css'
    css = css.replace('\\', "/")
    # HTML File Output
    #print 'Started Html file generated' + const.CRUMBS_TIMESTAMP
    html_file = open(os.getcwd() + const.MEDIA_UPLOADS + uploaded_file_name + '/output/' +
                     uploaded_file + '.html', "wb")
    #print(html_file)
    html_file.write('<style>')
    html_file.write(urllib.request.urlopen(css).read())
    html_file.write('</style>')
html\u text=markdown(html\u内容,输出格式='html4')
css=const.URL_SCHEME+“:/“+请求_主机+”/static/css/pdf.css”
css=css.replace(“\\”,“/”)
#HTML文件输出
#打印“生成的已启动Html文件”+const.CRUMBS\u时间戳
html_file=open(os.getcwd()+const.MEDIA_UPLOADS+UPLOADS_file_name+//output/'+
上传的_文件+'.html',“wb”)
#打印(html_文件)
html_file.write(“”)
html_file.write(urllib.request.urlopen(css.read())
html_file.write(“”)
错误日志:

Quit the server with CTRL-BREAK.
('Unexpected error:', <class 'TypeError'>)
Traceback (most recent call last):
  File "C:\Dev\EXE\crumbs_alteryx\alteryx\views.py", line 937, in parser
    result_upload['filename'])
  File "C:\Dev\EXE\crumbs_alteryx\alteryx\views.py", line 767, in generate_html
    html_file.write('<style>')
TypeError: a bytes-like object is required, not 'str'
使用CTRL-BREAK退出服务器。
(“意外错误:”,)
回溯(最近一次呼叫最后一次):
解析器中第937行的文件“C:\Dev\EXE\crumbs\u alteryx\alteryx\views.py”
结果(上传['filename'])
文件“C:\Dev\EXE\crumbs\u alteryx\alteryx\views.py”,第767行,在generate\u html中
html_file.write(“”)
TypeError:需要类似字节的对象,而不是“str”

如何更改您的

uploaded_file + '.html', "wb")

然后你需要转换你下面的行

   html_file.write(urllib.request.urlopen(css).read())


因为目前它是byte类型的

如何更改您的

uploaded_file + '.html', "wb")

然后你需要转换你下面的行

   html_file.write(urllib.request.urlopen(css).read())


因为当前的类型是byte

No change Mr@johnlthanks Mr@johnll为您的有价值的回答No change Mr@johnlthanks Mr@johnll为您的有价值的回答这是因为您以写入字节模式打开了文件。Python3明确区分了
字节
字符串和文本字符串。这可以防止Python2所允许的大量草率的字符串处理;这也使得Unicode处理更加理智。一开始可能看起来很烦人,但你们很快就会习惯它,并开始欣赏它。这是因为你们已经在写字节模式下打开了文件。Python3明确区分了
字节
字符串和文本字符串。这可以防止Python2所允许的大量草率的字符串处理;这也使得Unicode处理更加理智。一开始可能看起来很烦人,但你很快就会习惯,并开始欣赏它。