小编Tom*_* B.的帖子

Python YAML 到 JSON 到 YAML

我是 python 的新手,所以我正在构建一个简单的程序来将 YAML 解析为 JSON,并将 JSON 解析为 YAML。

yaml2json上一行YAML转换到JSON,但JSON验证说,这是正确的。

到目前为止,这是我的代码:

def parseyaml(inFileType, outFileType):
   infile = input('Please enter a {} filename to parse: '.format(inFileType))
   outfile = input('Please enter a {} filename to output: '.format(outFileType))

   with open(infile, 'r') as stream:
       try:
           datamap = yaml.safe_load(stream)
           with open(outfile, 'w') as output:
               json.dump(datamap, output)
       except yaml.YAMLError as exc:
           print(exc)

    print('Your file has been parsed.\n\n')


def parsejson(inFileType, outFileType):
   infile = input('Please enter a {} filename to parse: '.format(inFileType))
   outfile = input('Please enter a …
Run Code Online (Sandbox Code Playgroud)

python json yaml

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

json ×1

python ×1

yaml ×1