md 转换 json 翻译文件

时间:2022-06-19
本文章向大家介绍md 转换 json 翻译文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import collections

def readMd(mdfile):
    with open(mdfile) as f:
        lines = f.readlines()
        lines = list(map(lambda l: l.rstrip(), lines))
        d = collections.OrderedDict({line: '' for line in lines})
        json_str = json.dumps(d)

        return json_str

def readJson(json_str):
    ordered_dict = json.loads(json_str, object_pairs_hook=collections.OrderedDict)
    print(ordered_dict)

if __name__ == '__main__':
    json_str = readMd('README.md')
    readJson(json_str)