gpt4 book ai didi

python - 终端表 asciitables 和编码

转载 作者:行者123 更新时间:2023-12-01 02:42:03 25 4
gpt4 key购买 nike

所以我试图弄清楚如何在终端表中正确编码 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com