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="[
{"key":10,"value":"10 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":11,"value":"11 - 7 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":12,"value":"12 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":13,"value":"13 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"}]">
<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.
>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="[
{"key":10,"value":"10 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":11,"value":"11 - 7 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":12,"value":"12 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"},
{"key":13,"value":"13 - 0 \u05de\u05e7\u05d5\u05de\u05d5\u05ea \u05e4\u05e0\u05d5\u05d9\u05d9\u05dd"}]">
<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'}