0

I have this code that takes an XML file, takes the child elements (text tag) of the new_line tag and stores their index as a key in a dictionary, and the elements as values in the same dictionary. I want to delete the keys of the dictionary whose values contain "10.238", but it doesn't seem to work. Everything else works. This is my code:

import re
from xml.dom import minidom
from xml.etree import ElementTree as ET


def filter_values_by_keyword(my_dict, filter_by):
    """
    Return a list of values which contains `filter_by` keyword.

    Arguments:
        my_dict (dict): Dict containing (...data specifics here)
        filter_by (str): Keyword to look for in values of my_dict

    Return:
        List of filtered values
    """
    return [key for key, value in my_dict.items() if filter_by in value]


def get_xml_by_tag_names(xml_path, tag_name_1, tag_name_2):
    """
    Your docstring here.
    """
    data = {}
    xml_tree = minidom.parse(xml_path)
    item_group_nodes = xml_tree.getElementsByTagName(tag_name_1)
    for idx, item_group_node in enumerate(item_group_nodes):
        cl_compile_nodes = item_group_node.getElementsByTagName(tag_name_2)
        for _ in cl_compile_nodes:
            data[idx]=[item_group_node.toxml()]
    return data


def main():
    data = get_xml_by_tag_names('output2.xml', 'new_line', 'text')
    filtered_values = filter_values_by_keyword(data, '10.238')

    for item in filtered_values:
        del data[item]

    for value in data.values():
        myxml = ' '.join(value)
        # print(myxml)

        tree = ET.fromstring(myxml)
        lista = ([text.text for text in tree.findall('text')])
        testo = (' '.join(lista))

        print(testo)


if __name__ == "__main__":
    main()

And this is a sample of the XML:

    <pages>
      <page id="1" bbox="0.000,0.000,462.047,680.315" rotate="0">
        <textbox id="0" bbox="191.745,592.218,249.042,603.578">
    <textline>
         <new_line>
                  <text font="NUMPTY+ImprintMTnum" bbox="297.284,540.828,300.188,553.310" colourspace="DeviceGray" ncolour="0" size="12.482">della quale non conosce che una parte;] </text>
                  <text font="PYNIYO+ImprintMTnum-Italic" bbox="322.455,540.839,328.251,553.566" colourspace="DeviceGray" ncolour="0" size="12.727">prima</text>
                  <text font="NUMPTY+ImprintMTnum" bbox="331.206,545.345,334.683,552.834" colourspace="DeviceGray" ncolour="0" size="7.489">1</text>
                  <text font="NUMPTY+ImprintMTnum" bbox="177.602,528.028,180.850,540.510" colourspace="DeviceGray" ncolour="0" size="12.482">che nonconosce ancora appieno;</text>
                  <text font="NUMPTY+ImprintMTnum" bbox="189.430,532.545,192.908,540.034" colourspace="DeviceGray" ncolour="0" size="7.489">2</text>
                  <text font="NUMPTY+ImprintMTnum" bbox="203.879,528.028,208.975,540.510" colourspace="DeviceGray" ncolour="0" size="12.482">che</text>
                </new_line>
    </textline>
<textline bbox="68.032,408.428,372.762,421.166">
<new_line>
          <text font="NUMPTY+ImprintMTnum" bbox="307.143,408.428,310.392,420.910" colourspace="DeviceGray" ncolour="0" size="12.482">viso] vi</text>
          <text font="NUMPTY+ImprintMTnum" bbox="310.280,408.808,313.243,419.046" colourspace="DeviceGray" ncolour="0" size="10.238">-</text>
          <text font="PYNIYO+ImprintMTnum-Italic" bbox="320.072,408.439,325.868,421.166" colourspace="DeviceGray" ncolour="0" size="12.727">su</text>
          <text font="NUMPTY+ImprintMTnum" bbox="328.829,408.428,338.452,420.910" colourspace="DeviceGray" ncolour="0" size="12.482">m</text>
        </new_line>
</textline>
    </textbox>
    </page>
    </pages>
Anna
  • 369
  • 2
  • 10
  • Check the value of `filtered_values` after `filtered_values = filter_values_by_keyword(data, '10.238')` – this should shed some light on the problem. – Błotosmętek Apr 21 '20 at 15:56
  • Yes, it is an empty list! – Anna Apr 21 '20 at 15:58
  • Have you looked at this [question](https://stackoverflow.com/questions/29218750/what-is-the-best-way-to-remove-a-dictionary-item-by-value-in-python)? – CoderCat Apr 21 '20 at 16:03
  • So now check the value of `data` to see why `'10.238'` isn't found in there – there are three possibilities: 1) there simply isn't anything like that in `data`; 2)you're looking for a string and your `data` contains a number; 3) the string `'10.238'` is there, but in a subdict/sublist, not directly as a value in the `data` dict. – Błotosmętek Apr 21 '20 at 16:03
  • Where does the value 10.238 come from? It is not in the XML. – mzjn Apr 21 '20 at 16:08
  • So I checked it and the values in data are strings. However, they are written like tag attributes, so the dict value is structured like: `'size="10.238"'`. Could this be the problem and how can I solve this? – Anna Apr 21 '20 at 16:08
  • It is in the actual XML, it's a number in the `size` attribute of the tag. Example: `-` – Anna Apr 21 '20 at 16:09
  • Done, it should be all right now! I've edited my question, let me know if it helps. – Anna Apr 21 '20 at 16:15

1 Answers1

0

Give you another example that may help you.

from simplified_scrapy import SimplifiedDoc,req,utils
html = '''
Your xml
'''
doc = SimplifiedDoc(html)
# texts = doc.getElements('text').notContains('-',attr='text').text # Filter with text
texts = doc.getElements('text').notContains('10.238',attr='size').text # Filter with size
print(texts)

Result:

['della quale non conosce che una parte;]', 'prima', '1', 'che nonconosce ancora appieno;', '2', 'che', 'viso] vi', 'su', 'm']

Here are more examples. https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

dabingsou
  • 2,469
  • 1
  • 5
  • 8