[英]How to use json.tool from the shell to validate and pretty-print language files without removing the unicode?
Ubuntu 16.04
重击4.4
python 3.5
我从Upwork的翻译那里收到了一堆语言文件,但没有一个文件有相同的行数。 因此,我决定对它们进行验证和漂亮打印,因为它们采用.json格式,然后查看每个文件中缺少哪些行,因此我制作了一个简单的脚本来验证和漂亮打印:
#!/bin/sh
for file in *.json; do
python -m json.tool "${file}" > "${file}".tmp;
rm -f "${file}";
mv "${file}".tmp "${file}"
done
现在我的俄语Langauge文件看起来像:
"manualdirections": "\u041c\u0430\u0440\u0448\u0440\u0443\u0442",
"moreinformation": "\u0414\u0435\u0442\u0430\u043b\u0438",
"no": "\u041d\u0435\u0442",
我非常想保持文件内容不变。
这在json.tool
是不可能的:
https://github.com/python/cpython/blob/3.5/Lib/json/tool.py#L45
调用json.dumps
不允许传递关键字参数ensure_ascii=False
,这将在这里解决您的问题。
您将必须编写自己的json.tool
,对其进行monkeypatch或使用第三方代码。
编辑:我建议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))
注意open()
使用的encoding
。 这应该导入文件并根据需要显示它们。 我认为。
您可以改用以下等效的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.