当我在quarto gfm报告中显示一个pandas表时,我在github中查看报告时会看到表上方的html代码.我该如何预防这种情况?

enter image description here

Code from Qmd file that created the above report

---
title: 'HTML junk'
author: 'Joseph Powers'
date: 2024-03-14
format: gfm
---

```{python}
import numpy as np
import pandas as pd
```

# Notice the html code above the table
```{python}
N = int(5e3)
TRIALS = int(1)

pd.DataFrame(
    {
        "A": np.random.binomial(TRIALS,  0.65, N),
        "B": np.random.binomial(TRIALS,  0.65, N),
        "C": np.random.binomial(TRIALS,  0.65, N),
        "D": np.random.binomial(TRIALS,  0.67, N)
    }
)
```

推荐答案

使用print(pd.DataFrame.to_markdown()),以Markdown友好的格式打印DataFrame,并使用quarto chunk选项output: asis来获得原始Markdown表,而无需任何预处理.在呈现源qmd文件之后,您将看到生成的markdown文件这次不包含任何HTML代码.

---
title: 'HTML junk'
author: 'Joseph Powers'
date: 2024-03-14
format: gfm
---

```{python}
import numpy as np
import pandas as pd
```

# Notice the html code above the table
```{python}
#| output: "asis"
N = int(5e3)
TRIALS = int(1)

df = pd.DataFrame(
    {
        "A": np.random.binomial(TRIALS,  0.65, N),
        "B": np.random.binomial(TRIALS,  0.65, N),
        "C": np.random.binomial(TRIALS,  0.65, N),
        "D": np.random.binomial(TRIALS,  0.67, N)
    }
)

print(df.to_markdown())
```

Python相关问答推荐

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

对Numpy函数进行载体化

pandas DataFrame GroupBy.diff函数的意外输出

删除最后一个pip安装的包

Python上的Instagram API:缺少client_id参数"

用Python解密Java加密文件

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

多处理队列在与Forking http.server一起使用时随机跳过项目

Polars asof在下一个可用日期加入

在Python中使用if else或使用regex将二进制数据如111转换为001""

Python Pandas—时间序列—时间戳缺失时间精确在00:00

找到相对于列表索引的当前最大值列表""

OpenCV轮廓.很难找到给定图像的所需轮廓

Pandas—MultiIndex Resample—我不想丢失其他索引的信息´

以异步方式填充Pandas 数据帧

如何编辑此代码,使其从多个EXCEL文件的特定工作表中提取数据以显示在单独的文件中

设置索引值每隔17行左右更改的索引

如何在Python中创建仅包含完整天数的月份的列表

如何在python tkinter中绑定键盘上的另一个回车?

解析CSV文件以将详细信息添加到XML文件