Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Extract Pairs to Dictionary?

Given input like this:

<span class="inplaceeditable inplaceeditable-select" data-inplaceeditable="1" data-component="local_tregister" data-itemtype="cart_item" data-itemid="236756"

    data-value="12" data-editlabel="קבוצת רישום" data-type="select" data-options="[
    {&quot;key&quot;:10,&quot;value&quot;:&quot;10 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:11,&quot;value&quot;:&quot;11 - 7 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:12,&quot;value&quot;:&quot;12 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:13,&quot;value&quot;:&quot;13 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;}]">
        
        <a href="#" class="quickeditlink aalink" data-inplaceeditablelink="1" title="קבוצת רישום">
            12 - 0 מקומות פנויים
            <span class="quickediticon visibleifjs"><i class="icon fa fa-pencil fa-fw "  title="קבוצת רישום" aria-label="קבוצת רישום"></i></span>
        </a>
</span>

What’s the most efficient way (performance is critical to me) to get the following output:

{10:0, 11:7, 12:0, 13:0}

Please Note, I wrote them as numbers for ease of use only but I’m expecting key and value to be strings inside a dictionary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use regex matching with a lookbehind on ; to only catch the relevant data:

import re

data = '''<span class="inplaceeditable inplaceeditable-select" data-inplaceeditable="1" data-component="local_tregister" data-itemtype="cart_item" data-itemid="236756"

    data-value="12" data-editlabel="קבוצת רישום" data-type="select" data-options="[
    {&quot;key&quot;:10,&quot;value&quot;:&quot;10 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:11,&quot;value&quot;:&quot;11 - 7 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:12,&quot;value&quot;:&quot;12 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;},
    {&quot;key&quot;:13,&quot;value&quot;:&quot;13 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd&quot;}]">
        
        <a href="#" class="quickeditlink aalink" data-inplaceeditablelink="1" title="קבוצת רישום">
            12 - 0 מקומות פנויים
            <span class="quickediticon visibleifjs"><i class="icon fa fa-pencil fa-fw "  title="קבוצת רישום" aria-label="קבוצת רישום"></i></span>
        </a>
</span>'''


dict(i.split(' - ') for i in re.findall('(?<=;)(\d+\s-\s\d+)', data))

output:

{'10': '0', '11': '7', '12': '0', '13': '0'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading