import csv def escape_lua_string(value): """ Escape double quotes and backslashes for Lua strings. Lua uses \" to escape double quotes and \\ for backslashes. """ return value.replace("\\", "\\\\").replace("'", "\\'") def csv_to_lua_table(input_csv, output_lua): # Define the fields we care about fields_to_include = ["name", "y", "x", "ID", "ContinentID"] with open(input_csv, mode='r', newline='', encoding='utf-8') as csv_file: # Assuming comma-delimited CSV csv_reader = csv.DictReader(csv_file) with open(output_lua, mode='w', encoding='utf-8') as lua_file: lua_file.write("flightPoints = {\n") for row in csv_reader: lua_file.write(" {\n") for field in fields_to_include: value = row[field] # Escape and format Lua strings with single quotes if field == "name": if "'" in value: lua_value.replace("'","\'") lua_value = f"'{escape_lua_string(value)}'" # Handle numeric fields elif value.replace('.', '', 1).isdigit() or (value[0] == '-' and value[1:].replace('.', '', 1).isdigit()): lua_value = value else: lua_value = f"'{value}'" lua_file.write(f" {field} = {lua_value},\n") lua_file.write(" },\n") lua_file.write("}\n\n") # Input CSV file and output Lua file paths input_csv = "flight_points.csv" output_lua = "flight_points.lua" # Convert CSV to Lua table csv_to_lua_table(input_csv, output_lua) print(f"Lua table has been written to {output_lua}")