parser

 avatar
unknown
python
2 months ago
1.7 kB
3
Indexable
import json

# Путь к входному файлу
input_file_path = 'LMG_M27IAR.con'
# Путь к выходному файлу
output_file_path = 'LMG_M27IAR.json'


# Функция для парсинга файла
def parse_weapon_config(file_path):
    weapon_data = {}
    current_category = "general"

    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            # Пропускаем комментарии и пустые строки
            if line.startswith('rem') or not line:
                continue

            # Проверяем, если строка задает новую категорию
            if line.startswith('ObjectTemplate.createComponent'):
                _, component = line.split(' ', 1)
                current_category = component
                if current_category not in weapon_data:
                    weapon_data[current_category] = {}
                continue

            # Разделяем строку на ключ и значение
            if ' ' in line:
                key, value = line.split(' ', 1)
                if current_category not in weapon_data:
                    weapon_data[current_category] = {}
                weapon_data[current_category][key] = value

    return weapon_data

# Парсим файл
weapon_data = parse_weapon_config(input_file_path)

# Сохраняем данные в JSON-файл
with open(output_file_path, 'w', encoding='utf-8') as json_file:
    json.dump(weapon_data, json_file, indent=4, ensure_ascii=False)

print(f"Данные успешно сохранены в {output_file_path}")
Leave a Comment