作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图弄清楚如何在终端表中正确编码 .table
函数,但到目前为止还没有运气。
# -*- 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”编解码器无法解码位置 0 中的字节 0xc3:数据意外结束
最佳答案
documentation for asciitable指出 AsciiData
的参数应该是字符串列表的列表。
将 test_list
的定义更改为 test_list = [[test]]
会为我打印出一个表格。
从 test
的定义中删除 encode('utf-8')
使其可以很好地显示在我的终端上。
关于python - 终端表 asciitables 和编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589438/
所以我试图弄清楚如何在终端表中正确编码 .table 函数,但到目前为止还没有运气。 # -*- coding: utf-8 -*- from bs4 import BeautifulSoup imp
我是一名优秀的程序员,十分优秀!