2017-08-09 21 views
1

所以我想弄清楚如何在terminaltables中正确编码.table函数,但到目前为止没有运气。terminaltables asciitables and encoding

# -*- 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

documentation for asciitable状态的参数AsciiData应该是字符串列表清单。

将您的test_list的定义更改为test_list = [[test]]为我打印出一张表格。

test的定义中删除encode('utf-8')使其在我的终端上很好地显示。