md 转换 json 翻译文件

2019-04-17 17:05:27 浏览数 (1)

代码语言:javascript复制
#!/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)

0 人点赞