python - 如何在不删除 unicode 的情况下使用 shell 中的 json.tool 来验证和美化语言文件?

标签 python json bash

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/

相关文章:

python - 更新神经网络中的权重

javascript - 如何将图像作为数据放入json中

git - 有效地将项目添加到 git 索引

python - 用Python发送邮件主题

python - 如何将 GIF 动画写入 matplotlib 中的类文件缓冲区?

javascript - 如何在 JavaScript 中使用其函数之外的值?

bash 获取最后一行的第一个字

python - 如何使用 Python 打开新的 bash 终端

python - 从列表中查找矩阵中的字符串元素

python - 如何在 Python 中从 pandas 数据框创建嵌套的 JSON