当我尝试在 Emacs(Python 和 Elpy 环境)中打印 Polars DataFrame 时,收到以下错误消息:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 15-27: character maps to <undefined>
这是发生错误的最小示例:
import polars as pl
df = pl.DataFrame({
"A": [1, 2, 3],
"B": [3, 2, 1],
})
print(df)
这可能是一个简单的编码问题,但是我不知道如何解决它。在我的
init.el
中,我已经做了以下条目,但这并没有带来预期的成功。
(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-language-environment 'utf-8)
(set-selection-coding-system 'utf-8)
相同的代码,在
cmd.exe
中运行会产生这样一个看起来干净的表格。
┌─────┬─────┐
│ A ┆ B │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1 ┆ 3 │
│ 2 ┆ 2 │
│ 3 ┆ 1 │
└─────┴─────┘
polars.Config.set_tbl_formatting
更改或简化表格格式样式。
示例:
pl.Config.set_tbl_formatting("ASCII_FULL_CONDENSED")
print(df)
pl.Config.restore_defaults() # Optional
输出:
shape: (3, 2)
+-----+-----+
| A | B |
| --- | --- |
| i64 | i64 |
+===========+
| 1 | 3 |
| 2 | 2 |
| 3 | 1 |
+-----+-----+