python中如何区分代码块,python-markdown无法识别代码块?

I'm working on a script to transfer Markdown to HTML, I've tried both markdown and markdown2.

As I use MathJax to make it able to show math formulas in LaTex, I found markdown is better for me than markdown2.

However, both of them don't recognize code blocks in ```.

My code is written in Python.

My Markdown code is:

计算香农熵的函数:

```

from math import log

def calcShannonEnt(dataSet):

numEntries = len(dataSet) #类别个数

labelCount = {}

for featVec in dataSet: #对每一条数据

currentLabel = featVec[-1] #currentLabel为当前数据的类别

if currentLabel not in labelCount.keys(): #计数

labelCount[currentLabel] = 0

labelCount[currentLabel] += 1

shannonEnt = 0.0

for key in labelCount.keys():

prob = float(labelCount[key]) / float(numEntries)

shannonEnt -= prob * float(log(prob,2))#计算香农熵

return shannonEnt

```

使用要求:

- 调用的数据必须储存在列表中,且所有列表元素有相同长度

- 列表元素的最后一列为类别

[sorted函数及operator.itemgetter函数的用法详解](http://blog.csdn.net/alvine008/article/details/37757753

I hope those Chinese characters don't bother you.

The HTML code is :

计算香农熵的函数:

```

from math import log

def calcShannonEnt(dataSet):

numEntries = len(dataSet) #类别个数

labelCount = {}

for featVec in dataSet: #对每一条数据

currentLabel = featVec[-1] #currentLabel为当前数据的类别

if currentLabel not in labelCount.keys(): #计数

labelCount[currentLabel] = 0

labelCount[currentLabel] += 1

shannonEnt = 0.0

for key in labelCount.keys():

prob = float(labelCount[key]) / float(numEntries)

shannonEnt -= prob * float(log(prob,2))#计算香农熵

return shannonEnt

```

使用要求:

- 调用的数据必须储存在列表中,且所有列表元素有相同长度

- 列表元素的最后一列为类别

[sorted函数及operator.itemgetter函数的用法详解](http://blog.csdn.net/alvine008/article/details/37757753

What's the problem?

解决方案

With the help of @Waylan the problem has been solved perfectly.

It is because I didn't enable the extensions. See extensions

Now it is right:

html_txt = markdown.markdown(post.body_markdown, extensions=['fenced_code'])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值