Scipt:ExtractItemsByType: Difference between revisions
From The Walkscape Walkthrough
Content deleted Content added
CptRileyyy (talk | contribs) Add script |
CptRileyyy (talk | contribs) Add mediawiki html tags |
||
Line 4: | Line 4: | ||
# Path to the JSON file |
# Path to the JSON file |
||
json_file_path = '../json-data/ |
json_file_path = '../json-data/items_official_safe_3-9-2024.json' |
||
# Read the JSON file |
# Read the JSON file |
||
Line 37: | Line 37: | ||
output_file_path = os.path.join(output_dir, f'{obj_type}.json') |
output_file_path = os.path.join(output_dir, f'{obj_type}.json') |
||
with open(output_file_path, 'w') as output_file: |
with open(output_file_path, 'w') as output_file: |
||
output_file.write('<syntaxhighlight lang="json">\n') |
|||
json.dump(objects, output_file, indent=2) |
json.dump(objects, output_file, indent=2) |
||
output_file.write('\n</syntaxhighlight>') |
|||
output_file.write('\n<noinclude>[[Category:Data]]</noinclude>') |
|||
</syntaxhighlight> |
</syntaxhighlight> |
Latest revision as of 02:06, 10 March 2024
import json
import os
# Path to the JSON file
json_file_path = '../json-data/items_official_safe_3-9-2024.json'
# Read the JSON file
with open(json_file_path) as file:
data = json.load(file)
# Create a directory to store the output files
output_dir = '../wiki-json'
os.makedirs(output_dir, exist_ok=True)
print(f'Created directory: {output_dir}')
# Create a dictionary to hold the objects by type
objects_by_type = {}
# Iterate over the objects in the JSON file
for obj in data:
# Extract the value of the "type" key
obj_type = obj.get('type')
# Skip if "type" key is not present in the object
if not obj_type:
Warning(f'Object {obj} does not have a "type" key. Skipping...')
continue
# Add the object to the corresponding list in the dictionary
if obj_type not in objects_by_type:
objects_by_type[obj_type] = []
objects_by_type[obj_type].append(obj)
# Write each list of objects to a separate file
for obj_type, objects in objects_by_type.items():
output_file_path = os.path.join(output_dir, f'{obj_type}.json')
with open(output_file_path, 'w') as output_file:
output_file.write('<syntaxhighlight lang="json">\n')
json.dump(objects, output_file, indent=2)
output_file.write('\n
')
output_file.write('\n')
</syntaxhighlight>