如何在Python中从JSON文件中向列表中添加项目?

huangapple go评论93阅读模式
英文:

How can I add an item in a list from a JSON file in Python?

问题

  1. import json
  2. with open("example.json", "r") as jsonFile:
  3. data = json.load(jsonFile)
  4. data["language"].append('Spanish') # Append 'Spanish' to the 'language' list
  5. with open("example.json", "w") as jsonFile:
  6. json.dump(data, jsonFile)
英文:

I'm working with a JSON file and I was wondering if there's a way to append a string to a list within the file. Here's an example of the JSON file I'm working with:

  1. {"language": "['English', 'French']", "bank": 50}

I want to add the string "Spanish" to the "language" list. How can I do this?

Here's the code I've written so far, but I'm not sure how to modify it to achieve what I want:

  1. import json
  2. with open("example.json", "r") as jsonFile:
  3. data = json.load(jsonFile)
  4. add list(data["language"]['Spanish'])
  5. with open("example.json", "w") as jsonFile:
  6. json.dump(data, jsonFile)

How can I modify this code to achieve my goal?

答案1

得分: 2

  1. { "language": ["English", "French"], "bank": 50 }
  1. import json
  2. with open("temp.json", "r") as f:
  3. data = json.load(f)
  4. data["language"].append("Spanish")
  5. with open("temp.json", "w") as f:
  6. json.dump(data, f)
英文:

> {"language": "['English', 'French']", "bank": 50}

Here the "language" keys hold a string rather than a list because of the " before [ and " after ]. To solve this, change the file to this:

{"language": ["English", "French"], "bank": 50}

Then use this code to append "Spanish" or any language from now on:

  1. import json
  2. with open("temp.json", "r") as f:
  3. data = json.load(f)
  4. data["language"].append("Spanish")
  5. with open("temp.json", "w") as f:
  6. json.dump(data, f)

答案2

得分: 1

  1. import json
  2. import ast
  3. with open("example.json", "r") as jsonFile:
  4. data = json.load(jsonFile)
  5. #data={"language": "['English', 'French']", "bank": 50}
  6. #take a variable e
  7. e=ast.literal_eval(data['language'])
  8. e.append('Spanish')
  9. data['language']=e
  10. print(data)
  11. #{'language': ['English', 'French', 'Spanish'], 'bank': 50}
英文:
  1. import json
  2. import ast
  3. with open("example.json", "r") as jsonFile:
  4. data = json.load(jsonFile)
  5. #data={"language": "['English', 'French']", "bank": 50}
  6. #take a variable e
  7. e=ast.literal_eval(data['language'])
  8. e.append('Spanish')
  9. data['language']=e
  10. print(data)
  11. #{'language': ['English', 'French', 'Spanish'], 'bank': 50}

答案3

得分: 0

要向Python列表中添加元素,你应该使用append()方法。

示例:

  1. import ast
  2. ast.literal_eval(data["language"]).append("Spanish")
英文:

In order to add to a Python list you should use the append() method.

Example:

  1. import ast
  2. ast.literal_eval(data["language"]).append("Spanish")

huangapple
  • 本文由 发表于 2023年2月13日 22:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定