0

Python 3.8 スクリプトで 1 つの json 値を変更したい。

Python では、文字列は不変であるため、その文字をその場で変更することはできません。

これは私の多くを助けます:jsonファイルの値の一部を見つけて置き換える方法

TypeError: 'str' オブジェクトは項目の割り当てをサポートしていません

TypeError: 'str' object does not support item assignment item['hotKey'] = "" TypeError: 'str' オブジェクトは項目の割り当てをサポートしていません

脚本:item['hotKey'] = "<f11>"

from pathlib import Path
import json
home = str(Path.home())
path = home + "/.config/autokey/data/Sample Scripts/"
jsonFilePath = home + "/.config/autokey/data/Sample Scripts/.run-run-lintalistAHK-all.json"
with open(jsonFilePath) as f:
    data = json.load(f)
for item in data['hotkey']:
    item['hotKey'] = "<f11>"  # item['hotKey'].replace('$home', item['id'])
with open(jsonFilePath, 'w') as f:
    json.dump(data, f)

jsonファイル

{
     "hotkey": {
         "hotKey": "<f12>"
     },
}
4

1 に答える 1