• <bdo id='5E6i8'></bdo><ul id='5E6i8'></ul>

    1. <tfoot id='5E6i8'></tfoot>
      <i id='5E6i8'><tr id='5E6i8'><dt id='5E6i8'><q id='5E6i8'><span id='5E6i8'><b id='5E6i8'><form id='5E6i8'><ins id='5E6i8'></ins><ul id='5E6i8'></ul><sub id='5E6i8'></sub></form><legend id='5E6i8'></legend><bdo id='5E6i8'><pre id='5E6i8'><center id='5E6i8'></center></pre></bdo></b><th id='5E6i8'></th></span></q></dt></tr></i><div id='5E6i8'><tfoot id='5E6i8'></tfoot><dl id='5E6i8'><fieldset id='5E6i8'></fieldset></dl></div>

        <legend id='5E6i8'><style id='5E6i8'><dir id='5E6i8'><q id='5E6i8'></q></dir></style></legend>

        <small id='5E6i8'></small><noframes id='5E6i8'>

        在 Python 中解析 CS:GO 脚本文件

        时间:2023-10-09
        <i id='PnSyE'><tr id='PnSyE'><dt id='PnSyE'><q id='PnSyE'><span id='PnSyE'><b id='PnSyE'><form id='PnSyE'><ins id='PnSyE'></ins><ul id='PnSyE'></ul><sub id='PnSyE'></sub></form><legend id='PnSyE'></legend><bdo id='PnSyE'><pre id='PnSyE'><center id='PnSyE'></center></pre></bdo></b><th id='PnSyE'></th></span></q></dt></tr></i><div id='PnSyE'><tfoot id='PnSyE'></tfoot><dl id='PnSyE'><fieldset id='PnSyE'></fieldset></dl></div>
            <tbody id='PnSyE'></tbody>

          • <small id='PnSyE'></small><noframes id='PnSyE'>

          • <legend id='PnSyE'><style id='PnSyE'><dir id='PnSyE'><q id='PnSyE'></q></dir></style></legend>
              <tfoot id='PnSyE'></tfoot>

                  <bdo id='PnSyE'></bdo><ul id='PnSyE'></ul>

                  本文介绍了在 Python 中解析 CS:GO 脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在处理来自 CS:GO 的一些脚本文件,我必须从该文件中获取一些有用的信息并将这些数据导入我的 python 应用程序.

                  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.

                  这里是一个txt数据格式的例子:

                  Here is an example of the txt data format:

                  https://steamcdn-a.akamaihd.net/apps/730/scripts/items/items_game.83a9ad4690388868ab33c627af730c43d4b0f0d9.txt

                  这些值采用随机格式(ColorPosString),但我只需要一个包含所有值的字符串.我需要将此信息放入字典中,例如:

                  The values are in the random formats (ColorPosString), 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?

                  推荐答案

                  这是一个基于 pyparsing 的解析器,它将解析这种格式:

                  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)
                  

                  将配置加载到字符串中,并调用 parser.parseString():

                  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()
                  

                  打印:

                  ['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. ...
                  

                  编辑

                  如果您希望在解析时将整数值转换为整数,您可以定义解析操作来执行此操作.但是您只想将此(我认为)附加到作为值的引用字符串,而不是作为键的字符串.

                  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)
                  

                  现在输出值如下所示:

                  [['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 整数.

                  Note that the integer values are not displayed as strings any more, but as actual Python ints.

                  这篇关于在 Python 中解析 CS:GO 脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:扭曲的文档字符串中这些格式的含义是什么? 下一篇:格式:在字符串中使用大括号时出现 KeyError

                  相关文章

                  最新文章

                • <i id='7bFLd'><tr id='7bFLd'><dt id='7bFLd'><q id='7bFLd'><span id='7bFLd'><b id='7bFLd'><form id='7bFLd'><ins id='7bFLd'></ins><ul id='7bFLd'></ul><sub id='7bFLd'></sub></form><legend id='7bFLd'></legend><bdo id='7bFLd'><pre id='7bFLd'><center id='7bFLd'></center></pre></bdo></b><th id='7bFLd'></th></span></q></dt></tr></i><div id='7bFLd'><tfoot id='7bFLd'></tfoot><dl id='7bFLd'><fieldset id='7bFLd'></fieldset></dl></div>
                    <legend id='7bFLd'><style id='7bFLd'><dir id='7bFLd'><q id='7bFLd'></q></dir></style></legend>

                    <small id='7bFLd'></small><noframes id='7bFLd'>

                  1. <tfoot id='7bFLd'></tfoot>
                      • <bdo id='7bFLd'></bdo><ul id='7bFLd'></ul>