내가 목록을 포함하는이 JSON 파일이 좋아하는 오브젝트 : 나는 locales
광고 반복 (로케일에 드, FR, NL 등을 따라 vegetables
를 조작하고 싶은 으로 반복은
"locales": {
"de": {
"default": {
"vegetables": [
"apple",
"melon",
"grape",
"pear"
]
}
},
"fr": {
"default": {
"vegetables": [
"apple",
"melon",
"grape",
"pear"
]
}
},
"nl": {
"default": {
"vegetables": [
"apple",
"melon",
"grape",
"pear"
]
}
},
(...)
}
). 파이썬으로 어떻게하면 될까요?
나는 불행하게도이 작동하지 않습니다
import json
_MY_CUSTOM_ORDER_DE = [
"pear",
"grape",
"apple",
"melon"]
_MY_CUSTOM_ORDER_NL = [
"melon",
"pear",
"apple",
"grape"]
def updateJsonFile():
jsonFile = open('vegetables.json', 'r')
data = json.load(jsonFile)
jsonFile.close()
for item in data["locales"]:
if item == "de":
item["default"]["vegetables"] = _MY_CUSTOM_ORDER_DE
elif item == "nl":
item["default"]["vegetables"] = _MY_CUSTOM_ORDER_NL
else:
## do nothing ##
with open('sortedvegetables.json', 'w') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True)
def main():
updateJsonFile()
if __name__ == '__main__':
main()
같은 시도하지만 스크립트를 실행 한 후 터미널에 오류가 지금있다. sortedvegetables.json
정확히 같은 다음과 같은 vegetables.json
더 명확하게 표시 할 수 있습니까? 당신이 정말로보고있는 것을 이해할 수 없습니까? 몇 가지 명확한 예를 들어주세요! – iamnewuser
_MY_CUSTOM_ORDER_DE 및 _MY_CUSTOM_ORDER_NL도 표시합니다. – iamnewuser
어디에서 updateJsonFile을 호출합니까? –