通过去除引号和方括号显示pyparsing的结果

时间:2018-11-17 19:40:19

标签: python function pyparsing

如何从解析结果中删除引号和方括号,以便将其存储在数据库中以执行分析

python代码:

     #!/bin/python
     import pyparsing as pp
     from pyparsing import *
     import string


     # define the grammar for the log file
     integer = Word(nums)
     day = Word(alphas)

     # example log line
     log_display = ("[7]")

     # type of record
     ut_type = "[" + Word(nums) + "]"

     # combine the grammar
     log = ut_type 

     data = log.parseString(log_display)
     print(data)

     display_type = None

     def results(log):
          display_type = ut_type.setResultsName("Type of 
                         Record").setParseAction(removeQuotes)
          print(display_type)
          return display_type

     print("outside the function")
     print(display_type)

运行代码时输出:

     $python test.py 
     ['[', '7', ']']
     outside the function
     None

我想要一个输出:-

 Type of Record: 7

1 个答案:

答案 0 :(得分:-1)

您可以使用替换字符串功能从字符串中删除字符。

data.replace("[","").replace("]","").replace("\"","")