天天看點

Python程式設計:将markdown格式轉換為rst格式代碼示例

利用requests庫對網絡接口的請求,将markdown格式轉換為rst格式

代碼示例

# -*- coding: utf-8 -*-

# @File    : markdown_to_rst.py
# @Date    : 2018-08-20
# @Author  : Peng Shiyu


import requests


def md_to_rst(from_file, to_file):
    """
    将markdown格式轉換為rst格式
    @param from_file: {str} markdown檔案的路徑
    @param to_file: {str} rst檔案的路徑
    """
    response = requests.post(
        url='http://c.docverter.com/convert',
        data={'to': 'rst', 'from': 'markdown'},
        files={'input_files[]': open(from_file, 'rb')}
    )

    if response.ok:
        with open(to_file, "wb") as f:
            f.write(response.content)


if __name__ == '__main__':
    md_to_rst("README.md", "README.rst")
      

轉換結果:

README.md

# 文章标題

代碼示例
``
print("hello world!")
``      

備注:` 有三個,為了避免沖突隻寫了2個

README.rst

文章标題
========

代碼示例

::

    print("hello world!")