使用pyparsing

时间:2016-03-29 17:22:43

标签: python string pyparsing colon

这是数据:

C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK

我想得到这个结果

[C:/data/my_file.txt.c, 10, 0x21, name1, name2, 0x10, 1, OK]
[C:/data/my_file2.txt.c, 110, 0x1, name2, name5, 0x12, 1, NOT_OK]
[./data/my_file3.txt.c, 110, 0x1, name2, name5, 0x12, 10, OK]

我知道如何使用一些代码或字符串拆分和类似的东西,但我正在寻找一个使用pyparsing的好解决方案。我的问题是:/为文件路径。

附加问题我使用一些代码从记录中删除注释和其他内容,因此原始数据如下所示:

text = """C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
// comment
./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK
---- 
ok
"""

我在解析之前删除了“//”,“ok”和“---”

所以现在我还有下一个问题:

第一个问题的一些补充。直到现在我从数据文件中提取上面的行 - 这很有效。所以我逐行读取文件并解析它。但现在我发现可以使用parseFile来解析整个文件。所以我想我可以删除一些代码并使用parseFile代替。所以我要解析的文件有一个额外的页脚。

C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK: info message

-----------------------
3 Files 2 OK 1 NOT_OK
NOT_OK 

是否可以更改解析器以获得2个解析结果?

结果1:

[['C:/data/my_file.txt.c', '10', '0x21', 'name1', 'name2', '0x10', '1', 'OK'],
 ['C:/data/my_file2.txt.c', '110', '0x1', 'name2', 'name5', '0x12', '1', 'NOT_OK'],
 ['./data/my_file3.txt.c', '110', '0x1', 'name2', 'name5', '0x12', '10', 'OK']]

Ignore the blank line   
Ignore this line => -----------------------

结果2:

 [['3', 'Files', 2', 'OK’, '1', 'NOT_OK'],
 ['NOT_OK’],

所以我改变了thes Code:

    # define an expression for your file reference
one_thing = Combine(
    oneOf(list(alphas)) + ':/' +
    Word(alphanums + '_-./'))

# define a catchall expression for everything else (words of non-whitespace characters,
# excluding ':')
another_thing = Word(printables + " ", excludeChars=':')

# define an expression of the two; be sure to list the file reference first
thing = one_thing | another_thing

# now use plain old pyparsing delimitedList, with ':' delimiter
list_of_things = delimitedList(thing, delim=':')

list_of_other_things = Word(printables).setName('a')
# run it and see...
parse_ret = OneOrMore(Group(list_of_things | list_of_other_things)).parseFile("data.file")
parse_ret.pprint()

我得到了这个结果:

[['C:/data/my_file.txt.c', '10', '0x21', 'name1', 'name2', '0x10', '1', 'OK'],
['C:/data/my_file2.txt.c','110', '0x1', 'name2', 'name5', '0x12', '1', 'NOT_OK'],
['./data/my_file3.txt.c', '110', '0x1', 'name2', 'name5', '0x12', '10', 'OK', 'info message'],
['-----------------------'],
['3 Files 2 OK 1 NOT_OK'],
['NOT_OK']]

所以我可以使用它,但是可以将结果分成两个命名结果吗?我搜索了文档,但我找不到任何有用的东西。那顺便提一下有关于pyparsing的好教程吗?

感谢

3 个答案:

答案 0 :(得分:2)

请参阅嵌入式注释以了解pyparsing description:

from pyparsing import *

text = """C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
// blah-de blah blah blah
./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK"""

# define an expression for your file reference
one_thing = Combine(
    oneOf(list(alphas.upper())) + ':/' + 
    Word(alphanums + '_-./'))

# define a catchall expression for everything else (words of non-whitespace characters, 
# excluding ':')
another_thing = Word(printables, excludeChars=':')

# define an expression of the two; be sure to list the file reference first
thing = one_thing | another_thing

# now use plain old pyparsing delimitedList, with ':' delimiter
list_of_things = delimitedList(thing, delim=':')

parser = OneOrMore(Group(list_of_things))

# ignore comments starting with double slash
parser.ignore(dblSlashComment)

# run it and see...
parser.parseString(text).pprint()

打印:

[['C:/data/my_file.txt.c', '10', '0x21', 'name1', 'name2', '0x10', '1', 'OK'],
 ['C:/data/my_file2.txt.c', '110', '0x1', 'name2', 'name5', '0x12', '1', 'NOT_OK'],
 ['./data/my_file3.txt.c', '110', '0x1', 'name2', 'name5', '0x12', '10', 'OK']]

答案 1 :(得分:0)

所以我没有找到一个包含delimitedList和parseFile的解决方案,但我找到了一个对我来说没问题的解决方案。

from pyparsing import *

data = """
C: / data / my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C: / data / my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
./ data / my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK: info message

-----------------------
3 Files 2 OK 1 NOT_OK
NOT_OK
"""

if __name__ == '__main__':

# define an expression for your file reference
entry_one = Combine(
    oneOf(list(alphas)) + ':/' +
    Word(alphanums + '_-./'))

entry_two = Word(printables + ' ', excludeChars=':')
entry = entry_one | entry_two

delimiter = Literal(':').suppress()
tc_result_line = Group(entry.setResultsName('file_name') + delimiter + entry.setResultsName(
    'line_nr') + delimiter + entry.setResultsName('num_one') + delimiter + entry.setResultsName('name_one') + delimiter + entry.setResultsName(
    'name_two') + delimiter + entry.setResultsName('num_two') + delimiter + entry.setResultsName('status') + Optional(
    delimiter + entry.setResultsName('msg'))).setResultsName("info_line")

EOL = LineEnd().suppress()
SOL = LineStart().suppress()
blank_line = SOL + EOL

tc_summary_line = Group(Word(nums).setResultsName("num_of_lines") + "Files" + Word(nums).setResultsName(
    "num_of_ok") + "OK" + Word(nums).setResultsName("num_of_not_ok") + "NOT_OK").setResultsName(
    "info_summary")
tc_end_line = Or(Literal("NOT_OK"), Literal('Ok')).setResultsName("info_result")

# run it and see...
pp1 = tc_result_line | Optional(tc_summary_line | tc_end_line)
pp1.ignore(blank_line | OneOrMore("-"))

result = list()
for l in data.split('\n'):
    result.append((pp1.parseString(l)).asDict())
# delete empty results
result = filter(None, result)

for r in result:
    print(r)

pass

结果:

{'info_line': {'file_name': 'C', 'num_one': '10', 'msg': '1', 'name_one':   '0x21', 'line_nr': '/ data / my_file.txt.c', 'status': '0x10', 'num_two': 'name2', 'name_two': 'name1'}}
{'info_line': {'file_name': 'C', 'num_one': '110', 'msg': '1', 'name_one': '0x1', 'line_nr': '/ data / my_file2.txt.c', 'status': '0x12', 'num_two': 'name5', 'name_two': 'name2'}}
{'info_line': {'file_name': './ data / my_file3.txt.c', 'num_one': '0x1', 'msg': 'OK', 'name_one': 'name2', 'line_nr': '110', 'status': '10', 'num_two': '0x12', 'name_two': 'name5'}}
{'info_summary': {'num_of_lines': '3', 'num_of_ok': '2', 'num_of_not_ok': '1'}}
{'info_result': ['NOT_OK']}

答案 2 :(得分:-3)

使用re:

myList = ["C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK", "C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK", "./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK"]

for i in myList:
    newTxt =  re.sub(r':', ",", i)
    newTxt = re.sub(r',/', ":/", newTxt)
    print newTxt