Here is a pyparsing based parser that will parse this format:
from pyparsing import Suppress, QuotedString, Forward, Group, Dict, ZeroOrMore LBRACE,RBRACE = map(Suppress, "{}") qs = QuotedString('"')
Load the configuration into a line and call parser.parseString() :
sample = open('cs_go_sample.txt').read() config = parser.parseString(sample) print config.keys() for k in config.items_game.keys(): print '-', k config.items_game.pprint()
Print
['items_game'] - sticker_kits - client_loot_lists - prefabs - quest_definitions - alternate_icons2 - music_definitions - rarities - colors - campaign_definitions - player_loadout_slots - quest_schedule - item_levels - revolving_loot_lists - game_info - pro_players - recipes - items_game_live - paint_kits_rarity - paint_kits - qualities - items - attributes - item_sets - quest_reward_loot_lists - kill_eater_score_types [['game_info', ['first_valid_class', '2'], ['last_valid_class', '3'], ['first_valid_item_slot', '0'], ['last_valid_item_slot', '54'], ['num_item_presets', '4']], ['rarities', ['default', ['value', '0'], ... etc. ...
EDIT
If you want integer values ββto be converted to int during parsing, you can define the parsing action for this. But you want to attach this (I think) only to quoted strings, which are values, not those that are keys.
# use this code to convert integer values to ints at parse time key_qs = qs.copy() value_qs = qs.copy() def convert_integers(tokens): if tokens[0].isdigit(): tokens[0] = int(tokens[0]) value_qs.setParseAction(convert_integers) value = Forward() key_value = Group(key_qs + value) struct = LBRACE + Dict(ZeroOrMore(key_value)) + RBRACE value <<= value_qs | struct parser = Dict(key_value)
Now the output values ββlook like this:
[['game_info', ['first_valid_class', 2], ['last_valid_class', 3], ['first_valid_item_slot', 0], ['last_valid_item_slot', 54], ['num_item_presets', 4]], ['rarities', ['default', ['value', 0], ['loc_key', 'Rarity_Default'], ['loc_key_weapon', 'Rarity_Default_Weapon'],
Note that integer values ββare no longer displayed as strings, but as valid Python ints.