在python中操作json文件:更改key:value对的顺序

时间:2018-03-06 13:33:40

标签: python json dictionary

我有以下的json结构(见这里:https://github.com/sebischair/NLU-Evaluation-Corpora/blob/master/AskUbuntuCorpus.json):

{
        "author": "Kristofer",
        "url": "http://askubuntu.com/questions/226332/how-to-install-a-canon-mf8040cn-printer-on-ubuntu-12-04",
        "text": "How to Install a Canon MF8040Cn Printer on Ubuntu 12.04",
        "entities": [
            {
                "text": "Canon MF8040Cn",
                "entity": "Printer",
                "stop": 5,
                "start": 4
            },
            {
                "text": "12.04",
                "entity": "UbuntuVersion",
                "stop": 11,
                "start": 9
            }
        ],
        "intent": "Setup Printer",
        "answer": {
            "text": "<p>For 14.04 through 16.04 do the following:</p>\n\n<p>Download the drivers from:\n<a href=\"https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25\" rel=\"nofollow\">https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25</a></p>\n\n<p>untar it to a directory.\nfrom that directory (assuming 64-bit, adjust as you need to)</p>\n\n<pre><code>cd ~/Downloads\ngunzip -c Linux_UFRII_PrinterDriver_V320_us_EN.tar.gz | tar xvf -\ncd Linux_UFRII_PrinterDriver_V320_us_EN/64-bit_Driver/Debian/\nsudo apt-get -y install libglade2-0 libc6:i386 lib32z1 libxml2:i386 libjpeg62:i386 libstdc++6:i386\ncd 64-bit_Driver/Debian\nsudo dpkg -i *.deb\n</code></pre>\n\n<p>reboot</p>\n\n<p>run add printer .. should just show up automatically when you click the 'Add' button .. give it a few seconds and the printer chirps then just magically shows up.</p>\n\n<p>EDIT 3/2/15 for Vivid (15.04) (I suspect may need for 14.10 as well on brand new installation):</p>\n\n<p>Added extra software dependencies to the above</p>\n\n<p>EDIT 12/18/15 for 15.10 everything worked using above steps. Also, I didn't have to reboot .. it just worked.</p>\n\n<p>EDIT 2/27/16 (16.04beta) Updated the link to the 3.10 driver (they updated their website). Everything just worked using above, didn't need to reboot.</p>\n",
            "author": "JimB"
        },
        "training": false
    }

因此您可以使用json.load将其作为字典加载。

我需要更改实体字典中关键值对的顺序。现在它是文本,实体,停止,开始。我想改变每一对的顺序。 同样,在实体决定之后的“意图”我想在这个实体决定之前改变位置。

2 个答案:

答案 0 :(得分:0)

您可以使用OrderedDict来控制字典中键值对的顺序。

from collections import OrderedDict
import json

def ordered(d, desired_key_order):
    return OrderedDict([(key, d[key]) for key in desired_key_order])

toplevel_desired_key_order = ("author", "url", "text", "intent", "entities", "answer", "training")
entity_desired_key_order = ("stop", "start", "entity", "text")

with open("data.json") as file:
    d = json.load(file)

result = ordered(d, toplevel_desired_key_order)
result["entities"] = [ordered(entity, entity_desired_key_order) for entity in result["entities"]]

with open("results.json", "w") as file:
    json.dump(result, file, indent=4)

结果:

{
    "author": "Kristofer",
    "url": "http://askubuntu.com/questions/226332/how-to-install-a-canon-mf8040cn-printer-on-ubuntu-12-04",
    "text": "How to Install a Canon MF8040Cn Printer on Ubuntu 12.04",
    "intent": "Setup Printer",
    "entities": [
        {
            "stop": 5,
            "start": 4,
            "entity": "Printer",
            "text": "Canon MF8040Cn"
        },
        {
            "stop": 11,
            "start": 9,
            "entity": "UbuntuVersion",
            "text": "12.04"
        }
    ],
    "answer": {
        "text": "<p>For 14.04 through 16.04 do the following:</p>\n\n<p>Download the drivers from:\n<a href=\"https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25\" rel=\"nofollow\">https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25</a></p>\n\n<p>untar it to a directory.\nfrom that directory (assuming 64-bit, adjust as you need to)</p>\n\n<pre><code>cd ~/Downloads\ngunzip -c Linux_UFRII_PrinterDriver_V320_us_EN.tar.gz | tar xvf -\ncd Linux_UFRII_PrinterDriver_V320_us_EN/64-bit_Driver/Debian/\nsudo apt-get -y install libglade2-0 libc6:i386 lib32z1 libxml2:i386 libjpeg62:i386 libstdc++6:i386\ncd 64-bit_Driver/Debian\nsudo dpkg -i *.deb\n</code></pre>\n\n<p>reboot</p>\n\n<p>run add printer .. should just show up automatically when you click the 'Add' button .. give it a few seconds and the printer chirps then just magically shows up.</p>\n\n<p>EDIT 3/2/15 for Vivid (15.04) (I suspect may need for 14.10 as well on brand new installation):</p>\n\n<p>Added extra software dependencies to the above</p>\n\n<p>EDIT 12/18/15 for 15.10 everything worked using above steps. Also, I didn't have to reboot .. it just worked.</p>\n\n<p>EDIT 2/27/16 (16.04beta) Updated the link to the 3.10 driver (they updated their website). Everything just worked using above, didn't need to reboot.</p>\n",
        "author": "JimB"
    },
    "training": false
}

答案 1 :(得分:-1)

@Kevin:

我得到上面这个错误的数据示例:

$giving_back = array();

$givingback_details_funds = get_post_meta( $post->ID,'givingback_details_funds', true );

if ( $givingback_details_funds ) {
$giving_back[] = $givingback_details_funds;
}

$givingback_details_days = get_post_meta( $post->ID, 'givingback_details_days', true );

if ( $givingback_details_days ) {
$giving_back[] = _n( 'one working day', $givingback_details_days. ' working days', $givingback_details_days );
}

if ( ! empty( $giving_back ) ) {
echo 'the team donated' . implode( ' and ', $giving_back );
}