【问题标题】:Converting a dictionary to json having persian characters将字典转换为具有波斯字符的 json
【发布时间】:2019-02-03 08:31:43
【问题描述】:

这是我的一些代码,我正在尝试将字典转换为具有波斯字符的 json,但我得到的是问号而不是字符。我的字典是这样的:

bycommunity("0": [{"60357": "این یک پیام است"}] )

with open('data.json', 'wb') as f:
f.write(json.dumps(bycommunity).encode("utf-8"))

结果是:

{"0": [{"60357": "?????? ??? ??? ???? ???????? ??????"}]} 

【问题讨论】:

    标签: python json dictionary persian


    【解决方案1】:
    data = {"0": [{"60357": "این یک پیام است"}]} 
    with open('data.json', 'w') as f:
      json.dump(data, f, ensure_ascii=False)
    

    也可以查看Answer了解更多详情

    【讨论】:

    • 请不要将变量命名为dict,从而隐藏内置的dict。命名它,例如data,或其他。
    • 您也不需要显式关闭使用with 打开的文件。您可以使用json.dump(data, f, ensure_ascii=True) 来否定必须使用jsonData 变量。
    • 它有助于解释您所做的更改,而不是仅仅纠正它。
    【解决方案2】:
    with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: 
            jsonf.write(json.dumps(data, ensure_ascii=False, indent=4))
    

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多