簡體   English   中英

TypeError:需要一個類似字節的對象,而不是'str'python3

[英]TypeError: a bytes-like object is required, not 'str' python3

在我的項目中,我正在將樣式動態地寫入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>')

錯誤日志:

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'

如何改變你的

uploaded_file + '.html', "wb")

uploaded_file + '.html', "w")

然后您需要在下面轉換行

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

   html_file.write(urllib.request.urlopen(css).read().decode("utf-8"))

因為當前它是字節類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM