Ubuntu 16.04
bash 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
现在我的俄语语言文件如下所示:
"manualdirections": "\u041c\u0430\u0440\u0448\u0440\u0443\u0442",
"moreinformation": "\u0414\u0435\u0442\u0430\u043b\u0438",
"no": "\u041d\u0435\u0442",
我非常希望保持文件内容不变。
最佳答案
您可以改用以下等效的 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)
关于python - 如何在不删除 unicode 的情况下使用 shell 中的 json.tool 来验证和美化语言文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712674/