F1 circuit layouts with year-by-year SVGs — manually traced track variations
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

49 řádky
1.9KB

  1. import json
  2. def add_file_paths(data):
  3. if not isinstance(data, dict):
  4. return data
  5. for country_slug, country_data in data.items():
  6. if 'cities' in country_data:
  7. for city_slug, city_data in country_data['cities'].items():
  8. if 'circuits' in city_data:
  9. for circuit_slug, circuit_data in city_data['circuits'].items():
  10. if 'layouts' in circuit_data:
  11. for layout_years, layout_data in circuit_data['layouts'].items():
  12. layout_slug = layout_data.get('slug', layout_years)
  13. # Generate filePaths dictionary
  14. base_path = f"/{country_slug}/{city_slug}/{circuit_slug}/{layout_slug}"
  15. layout_data['filePaths'] = {
  16. "png": f"{base_path}.png",
  17. "svg": f"{base_path}.svg",
  18. "geojson": f"{base_path}.geo.json"
  19. }
  20. return data
  21. def main():
  22. try:
  23. # Read the JSON file with UTF-8 encoding
  24. with open('./circuits.json', 'r', encoding='utf-8') as file:
  25. data = json.load(file)
  26. # Process the data
  27. updated_data = add_file_paths(data)
  28. # Write the updated data back to the file with UTF-8 encoding and ensure_ascii=False
  29. with open('./circuits-new.json', 'w', encoding='utf-8') as file:
  30. json.dump(updated_data, file, indent=2, ensure_ascii=False)
  31. print("Successfully added file paths to layouts")
  32. except FileNotFoundError:
  33. print("Error: circuits.json file not found")
  34. except json.JSONDecodeError:
  35. print("Error: Invalid JSON format in circuits.json")
  36. except Exception as e:
  37. print(f"An error occurred: {str(e)}")
  38. if __name__ == "__main__":
  39. main()