私のプロジェクトでは、私は動的にHTMLファイルにスタイルを書いています。私は最近、ログの下に示すように、今ではエラーを投げ3にpython2から移行してきた:TypeError: 'str'ではなくバイトのようなオブジェクトが必要ですpython3
コードスニペット:
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'
あるので
に
下記のお行を変換する必要があります。 djangoとは何をするのですか? – Gahan
Python 3では、 'bytes'文字列とテキスト文字列を明確に区別しています。これにより、Python 2があなたに迷惑をかけることのないたくさんの文字列処理が防止されます。それはまた、Unicodeの扱いをより衛生的にします。最初は迷惑に見えるかもしれませんが、すぐにそれに慣れて、それを感謝するようになります。 –