简体   繁体   English

如何在不删除unicode的情况下从外壳中使用json.tool来验证和打印漂亮的语言文件?

[英]How to use json.tool from the shell to validate and pretty-print language files without removing the unicode?

Ubuntu 16.04 Ubuntu 16.04
Bash 4.4 重击4.4
python 3.5 python 3.5

I received a bunch of language files from the translators at Upwork and noticed none of the files had the same line count. 我从Upwork的翻译那里收到了一堆语言文件,但没有一个文件有相同的行数。 So I decided to validate and pretty-print them since they were in .json format and then see which lines were missing from each file, so I made a simple script to validate and pretty-print: 因此,我决定对它们进行验证和漂亮打印,因为它们采用.json格式,然后查看每个文件中缺少哪些行,因此我制作了一个简单的脚本来验证和漂亮打印:

#!/bin/sh

for file in *.json; do
   python -m json.tool "${file}" > "${file}".tmp;
   rm -f "${file}";
   mv "${file}".tmp "${file}"
done

Now my Russian Langauge file looks like: 现在我的俄语Langauge文件看起来像:

"manualdirections": "\u041c\u0430\u0440\u0448\u0440\u0443\u0442",
"moreinformation": "\u0414\u0435\u0442\u0430\u043b\u0438",
"no": "\u041d\u0435\u0442",

I would very much like to keep the content of the files untouched. 我非常想保持文件内容不变。

This is not possible in json.tool : 这在json.tool是不可能的:

https://github.com/python/cpython/blob/3.5/Lib/json/tool.py#L45 https://github.com/python/cpython/blob/3.5/Lib/json/tool.py#L45

The call to json.dumps does not allow to pass the keyword argument ensure_ascii=False which would solve your issue here. 调用json.dumps不允许传递关键字参数ensure_ascii=False ,这将在这里解决您的问题。

You will have to write your own json.tool , monkeypatch it, or use third-party code. 您将必须编写自己的json.tool ,对其进行monkeypatch或使用第三方代码。

edit: I've proposed PR 9765 to add this feature to json.tool in Python 3.8. 编辑:我建议PR 9765将此功能添加到Python 3.8中的json.tool中。

#!/usr/bin/python3

for filename in os.listdir('/path/to/json_files'):
    if filename.endswith('.json'):
        with open(filename, encoding='utf-8') as f:
            data = f.read()
            print(json.dumps(data, indent=4))

Notice the encoding used with open() . 注意open()使用的encoding This SHOULD import the files and display them as necessary. 这应该导入文件并根据需要显示它们。 I think. 我认为。

You can use the following equivalent Python script instead, which uses a subclass of json.JSONEncoder to override the ensure_ascii option: 您可以改用以下等效的Python脚本,该脚本使用json.JSONEncoder的子类来覆盖ensure_ascii选项:

import json
import os
import glob

class allow_nonascii(json.JSONEncoder):
    def __init__(self, *args, ensure_ascii=False, **kwargs):
        super().__init__(*args, ensure_ascii=False, **kwargs)

for file in glob.iglob('*.json'):
    with open(file, 'r') as fin, open(file + '.tmp', 'w') as fout:
        fout.write(json.dumps(json.load(fin), cls=allow_nonascii, indent=4))
        os.remove(file)
        os.rename(file + '.tmp', file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM