An open API service indexing awesome lists of open source software.

https://github.com/dotpot/custom-string-parser

Custom string data parser written in python
https://github.com/dotpot/custom-string-parser

Last synced: over 1 year ago
JSON representation

Custom string data parser written in python

Awesome Lists containing this project

README

          

Custom String Parser

You can use this component if you need to parse any information from any string value which has some syntax logics.

The easiest way to parse data from string in python.

Overview

CustomStringParser, the missing simple string parser for python developers.

Usage

Parsing HTML

Note: for html based parsing you should consider using xpath

Imagine you have this kind of content in your string_data with this content:

<div class="section-item">

<div class="section-title">
title1
</div> <!-- end section-title -->
<div class="section-comments">
15
</div> <!-- end section-comments -->
</div> <!--end section-item-->
<div class="section-item">
<div class="section-title">
title2
</div> <!-- end section-title -->
<div class="section-comments">
16
</div> <!-- end section-comments -->
</div> <!--end section-item-->
<div class="section-item">
<div class="section-title">
title3
</div> <!-- end section-title -->
<div class="section-comments">
17
</div> <!-- end section-comments -->
</div> <!--end section-item-->

We need to parse these items:


  • title

  • comments count

Code to parse this looks like this:

parser = CustomStringParserCore(string_data)

item_parser = ParsingNode('item', '<div class="section-item">', '</div> <!--end section-item-->')

title_parser = ParsingNode('title', '<div class="section-title">', '</div> <!-- end section title -->')
comments_parser = ParsingNode('comments', '<div class="section-comments">', '</div> <!-- end section-comments -->')
# note: our item result will have title and comments inside of it, so we can do this:
item_parser.add_parser(title_parser)
item_parser.add_parser(comments_parser)

# add main parser to the parsing core
parser.add_parser(item_parser)

# call the parse
parser.parse()

<..>

output (print_results(item_parser.results)):

item:

<div class="section-title">

title1
</div> <!-- end section-title -->
<div class="section-comments">
15
</div> <!-- end section-comments -->

title:

title1

comments:

15

item:

<div class="section-title">

title2
</div> <!-- end section-title -->
<div class="section-comments">
16
</div> <!-- end section-comments -->

title:

title2

comments:

16

item:

<div class="section-title">

title3
</div> <!-- end section-title -->
<div class="section-comments">
17
</div> <!-- end section-comments -->

title:

title3

comments:

17

This is very generic, so you can parse practically any structure.

Unit tests


This library suppose to be fully unit tested. So if you want to participate keep that in mind.

Feature ideas ( not yet implemented )


* Regex based parsers possibility.
* Grouped regex based parsers possibility.
* XPath based parsers possibility.
* Filtering out results by parser name.