2017-12-28 10 views
0

私のプロジェクトでは、私は動的に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' 
+0

あるので

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

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

下記のお行を変換する必要があります。 djangoとは何をするのですか? – Gahan

+0

Python 3では、 'bytes'文字列とテキスト文字列を明確に区別しています。これにより、Python 2があなたに迷惑をかけることのないたくさんの文字列処理が防止されます。それはまた、Unicodeの扱いをより衛生的にします。最初は迷惑に見えるかもしれませんが、すぐにそれに慣れて、それを感謝するようになります。 –

答えて

1

どのようにあなたの

の変更について
uploaded_file + '.html', "wb") 

~

uploaded_file + '.html', "w") 

、その後、あなたは現在、それはあなたが書き込みバイトモードでファイルを開いたためですbyte型

+0

変更なしMr @johnll –

+0

@SanuKyadav私の回答を更新しました – johnII

+0

あなたの貴重な答えに感謝したMr @johnll –

関連する問題