用Python解析CS:GO脚本文件 [英] Parsing a CS:GO script file in Python
问题描述
我正在使用CS:GO的一些脚本文件,我必须从这个文件中获取一些有用的信息,并将这些数据导入到我的python应用程序中。
是txt数据格式的一个例子:
这些值是随机格式(Color \ Pos \ String),但是我只需要一个包含所有值的字符串。
我需要得到这个信息到字典中,例如:
prerint $(global_dict ['items_game'] ['' game_info'] ['first_valid_class'])
<2
现在在解析器上工作,但是我遇到了很多问题。有没有任何现成的解决方案,该文件格式?
这是一个基于pyparsing的解析器,它将解析这种格式:
来自pyparsing import Suppress,QuotedString,Forward,Group,Dict,ZeroOrMore
$ b
LBRACE,RBRACE = map(Suppress,{})
qs = QuotedString('')
#前向声明值,因为这个表达式是递归的
#(将包含使用包含值的表达式)
value = Forward ()
key_value = Group(qs + value)
struct = LBRACE + Dict(ZeroOrMore(key_value))+ RBRACE
#使用< ;< =运算符
值<< = qs | struct
#define顶级解析器
解析器=字典(key_value)
将配置加载到一个字符串中,并调用
parser.parseString()
:sample = open('cs_go_sample.txt')。read()
config = parser.parseString(sample)
在config.it中打印config.keys()
ems_game.keys():
print' - ',k
config.items_game.pprint()
$ p
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b - client_loot_lists
- prefabs
- quest_definitions
- alternate_icons2
- music_definitions
- 稀有
- 颜色
- campaign_definitions
- player_loadout_slots
- quest_schedule
- item_levels
- revolving_loot_lists
- game_info
- pro_players
- 食谱
- items_game_live
- paint_kits_rarity
- paint_kits
- 品质
- 项目
- 属性
- 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'],
...等等...
编辑
如果您希望整数值在解析时转换为整数,您可以定义一个分析动作这个。但是你想附加这个(我认为)只是引用的字符串是值,而不是那些键。
#使用此代码在解析时将整数值转换为整数
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)
$ b $ value = Forward()
key_value = Group(key_qs + value)
struct = LBRACE + Dict(ZeroOrMore(key_value))+ RBRACE
value< = value_qs | struct
parser = Dict(key_value)
现在输出值如下所示:
[['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'],
请注意,整数值不再显示为字符串,而是作为实际的Python ints。
I am working with some script files from CS:GO, I have to get some useful information from this file and import this data into my python application.
Here is an example of the txt data format:
The values are in the random formats (Color\Pos\String), but i need just a string, which contains all of the values. I need to get this information into the dictionary for example:
print(global_dict['items_game']['game_info']['first_valid_class'])
<<2
I'm working on parser now, but I was faced with a lot of problems. Is there any ready solutions for that file format?
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('"')
# forward-declare value, since this expression will be recursive
# (will contain expressions which use contain value's)
value = Forward()
key_value = Group(qs + value)
struct = LBRACE + Dict(ZeroOrMore(key_value)) + RBRACE
# define content of value using <<= operator
value <<= qs | struct
# define top-level parser
parser = Dict(key_value)
Load the config into a string, 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()
Prints:
['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 the integer values to be converted to ints at parse time, you can define a parse action to do this. But you want to attach this (I think) only to the quoted strings that are values, not the ones 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:
[['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 the integer values are not displayed as strings any more, but as actual Python ints.
这篇关于用Python解析CS:GO脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!