在JSON文件中移除特定行

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

Removing Specific Lines in Json File

问题

I'm here to provide the translated code for you. It seems like you want to remove dictionary key-value pairs where the key is "Company" in the "Stores" list. Here's the code with the translation:

  1. import json
  2. # 打开 JSON 文件并加载数据
  3. with open("stores.json", 'r') as jf:
  4. jsonFile = json.load(jf)
  5. # 打印 JSON 数据的长度
  6. print(len(jsonFile))
  7. testJson = {}
  8. # 遍历 JSON 数据
  9. for k, v in jsonFile.items():
  10. # 如果不是 "Company" 键
  11. if k != "Company":
  12. for key in v:
  13. # 如果键不以 "Company" 开头
  14. if not key.startswith('Company'):
  15. print(key)
  16. testJson = jsonFile[key]
  17. # 打印 JSON 数据的长度
  18. print(len(jsonFile))

Please note that this code is a translation of your original code, but it may not achieve the desired result. If you want to remove key-value pairs where the key is "Company" within the "Stores" list, you would need to modify the code accordingly.

英文:

I am trying to clean the json file down below. I want to remove all the dict key value pairs for which the key is "Company" in the "Stores" list.

  1. {
  2. "Company": "Apple",
  3. "Stores": [
  4. {"Company" : "Apple",
  5. "Location" : "-",
  6. "Sales": "-",
  7. "Total_Employees": "-"
  8. },
  9. {"Company" : "Apple",
  10. "Location" : "-",
  11. "Sales": "-",
  12. "Total_Employees"
  13. },
  14. {"Company" : "Apple",
  15. "Location" : "-",
  16. "Sales": "-",
  17. "Total_Employees": "-"
  18. },
  19. ]

}

This is my code so far:

  1. import json
  2. with open("stores.json", 'r') as jf:
  3. jsonFile = json.load(jf)
  4. print(len(jsonFile))
  5. testJson = {}
  6. for k, v in jsonFile.items():
  7. for key in v:
  8. if not key.startswith('Company'):
  9. print(key)
  10. testJson = jsonFile[key]
  11. print(len(jsonFile))

When I run it im getting TypeError: 'int' object is not iterable.

答案1

得分: 1

首先,你的 JSON 格式有误,缺少 "Total_employee" 的值。你可以按照以下方式进行修改:

  1. {
  2. "Company": "Apple",
  3. "Stores": [
  4. {
  5. "Company": "Apple",
  6. "Location": "-",
  7. "Sales": "-",
  8. "Total_Employees": ""
  9. },
  10. {
  11. "Company": "Apple",
  12. "Location": "-",
  13. "Sales": "-",
  14. "Total_Employees": ""
  15. },
  16. {
  17. "Company": "Apple",
  18. "Location": "-",
  19. "Sales": "-",
  20. "Total_Employees": ""
  21. }
  22. ]
  23. }

然后,回答你的第一个问题:

  1. import json
  2. # 从文件中读取 JSON 数据
  3. with open('store.json', 'r') as file:
  4. json_data = json.load(file)
  5. if 'Stores' in json_data:
  6. stores = json_data['Stores'] # 获取 Stores 字典
  7. for store in stores:
  8. if 'Company' in store: # 验证是否存在 Company 键
  9. del store['Company'] # 删除 Stores 内的 Company 键
  10. # 将修改后的数据转换回 JSON 字符串
  11. updated_json_string = json.dumps(json_data, indent=4)
  12. # 将更新后的数据写入文件
  13. with open('updated_data.json', 'w') as file:
  14. file.write(updated_json_string)

注意:我已经忽略了代码部分,只提供了翻译好的内容。如果你需要进一步的解释或帮助,请随时提出。

英文:

First off, your json is wrong, it's missing values for Total_employee

you can modify it the following way :

  1. {
  2. "Company": "Apple",
  3. "Stores": [
  4. {"Company" : "Apple",
  5. "Location" : "-",
  6. "Sales": "-",
  7. "Total_Employees": ""
  8. },
  9. {"Company" : "Apple",
  10. "Location" : "-",
  11. "Sales": "-",
  12. "Total_Employees" : ""
  13. },
  14. {"Company" : "Apple",
  15. "Location" : "-",
  16. "Sales": "-",
  17. "Total_Employees": ""
  18. }
  19. ]
  20. }

Then, to answer your first question

  1. with open('store.json', 'r') as file:
  2. json_data = json.load(file)
  3. if 'Stores' in json_data:
  4. stores = json_data['Stores'] # Gets Stores dictionnary
  5. for store in stores:
  6. if 'Company' in store: # Verify if company key exist
  7. del store['Company'] # Delete company key inside Stores
  8. # Convert the modified data back to JSON string
  9. updated_json_string = json.dumps(json_data, indent=4)
  10. with open('updated_data.json', 'w') as file:
  11. file.write(updated_json_string)

答案2

得分: 0

以下是您要翻译的内容:

对问题中显示的数据进行更改(以使其成为有效的JSON),您可以执行以下操作:

  1. import json
  2. import os
  3. FILENAME = '/Volumes/G-Drive/foo.json'
  4. with open(FILENAME, 'r+') as data:
  5. d = json.load(data)
  6. for store in d.get('Stores', []):
  7. try:
  8. del store['Company']
  9. except KeyError:
  10. pass
  11. data.seek(0, os.SEEK_SET)
  12. json.dump(d, data, indent=2)
  13. data.truncate()
英文:

Having made changes to the data shown in the question (to make it valid JSON) you could do this:

  1. import json
  2. import os
  3. FILENAME = '/Volumes/G-Drive/foo.json'
  4. with open(FILENAME, 'r+') as data:
  5. d = json.load(data)
  6. for store in d.get('Stores', []):
  7. try:
  8. del store['Company']
  9. except KeyError:
  10. pass
  11. data.seek(0, os.SEEK_SET)
  12. json.dump(d, data, indent=2)
  13. data.truncate()

huangapple
  • 本文由 发表于 2023年5月26日 16:37:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339087.html
匿名

发表评论

匿名网友

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

确定