2017-08-09 4 views
1

だから私はきちんとterminaltablesで.table機能が、これまでの運をエンコードする方法を把握しようとしています。terminaltablesのasciitablesとエンコーディング

# -*- coding: utf-8 -*- 
from bs4 import BeautifulSoup 
import re 
from terminaltables import AsciiTable 


html = """<a href="#" class="link" title="Some title B"> 
       Some text with åäö</a>""" 

soup = BeautifulSoup(html, "html.parser") 
test = soup.find('a').get_text(strip=True).encode('utf-8') 
test_list = [test] 
test_table = AsciiTable(test_list) 

print test_list 
print test 
print test_table.table 

これは

['Some text with \xc3\xa5\xc3\xa4\xc3\xb6'] 
Some text with åäö 

そしてtest_table.table

UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data

答えて

2

ためのトレースバックAsciiDataへの引数は文字列のリストのリストでなければなりませんdocumentation for asciitable状態を表示します。私にとってtest_listtest_list = [[test]]にプリントアウトテーブルのあなたの定義を変更する

testの定義からencode('utf-8')を削除すると、端末にうまく表示されます。

関連する問題