import argparse

from lxml import etree

argparser = argparse.ArgumentParser(description="Newspaper XML parser", fromfile_prefix_chars='@')

argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")

args = argparser.parse_args()
data_root = args.dataroot

with open(data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/1457-4616_1854-08-01_31_001.xml', 'r') as f:
    tree = etree.parse(f)

root = tree.getroot()
print(root.tag)

import argparse
import glob

from lxml import etree

argparser = argparse.ArgumentParser(description="Newspaper XML parser", fromfile_prefix_chars='@')
argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")
args = argparser.parse_args()

data_root = args.dataroot


def read_xml_directory(path):
    """
    Read XML files from path, parse them, and return them as list
    """
    files = glob.glob(path + "*.xml")

    xmls = []
    for xmlfile in files:
        with open(xmlfile, 'r') as f:
            xmls.append(etree.parse(f))

    return xmls


def find_by_block_id(xmls, block_id):
    """
    Find an element by block_id from a list of lxml trees
    """
    block_xpath = etree.XPath("//*[@ID='{id}']".format(id=block_id))
    for xml in xmls:
        elements = block_xpath(xml)
        if elements:
            return elements[0]


some_dir = data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/'
xmls = read_xml_directory(some_dir)

print(etree.tostring(find_by_block_id(xmls, 'P2_TB00001')))



Read XML files from a directory and find textblock by id
Read XML files from a directory and find textblock by id

  import argparse
+ import glob
  
  from lxml import etree
  
  argparser = argparse.ArgumentParser(description="Newspaper XML parser", fromfile_prefix_chars='@')
+ argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")
+ args = argparser.parse_args()
  
- argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")
- 
- args = argparser.parse_args()
  data_root = args.dataroot
  
- with open(data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/1457-4616_1854-08-01_31_001.xml', 'r') as f:
-     tree = etree.parse(f)
  
- root = tree.getroot()
- print(root.tag)
+ def read_xml_directory(path):
+     """
+     Read XML files from path, parse them, and return them as list
+     """
+     files = glob.glob(path + "*.xml")
  
+     xmls = []
+     for xmlfile in files:
+         with open(xmlfile, 'r') as f:
+             xmls.append(etree.parse(f))
+ 
+     return xmls
+ 
+ 
+ def find_by_block_id(xmls, block_id):
+     """
+     Find an element by block_id from a list of lxml trees
+     """
+     block_xpath = etree.XPath("//*[@ID='{id}']".format(id=block_id))
+     for xml in xmls:
+         elements = block_xpath(xml)
+         if elements:
+             return elements[0]
+ 
+ 
+ some_dir = data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/'
+ xmls = read_xml_directory(some_dir)
+ 
+ print(etree.tostring(find_by_block_id(xmls, 'P2_TB00001')))
+ 
+ 
+ 
Read XML files from a directory and find textblock by id
## Code Before:
import argparse

from lxml import etree

argparser = argparse.ArgumentParser(description="Newspaper XML parser", fromfile_prefix_chars='@')

argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")

args = argparser.parse_args()
data_root = args.dataroot

with open(data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/1457-4616_1854-08-01_31_001.xml', 'r') as f:
    tree = etree.parse(f)

root = tree.getroot()
print(root.tag)

## Instruction:
Read XML files from a directory and find textblock by id
## Code After:
import argparse
import glob

from lxml import etree

argparser = argparse.ArgumentParser(description="Newspaper XML parser", fromfile_prefix_chars='@')
argparser.add_argument("dataroot", help="Path to DHH 17 newspapers directory")
args = argparser.parse_args()

data_root = args.dataroot


def read_xml_directory(path):
    """
    Read XML files from path, parse them, and return them as list
    """
    files = glob.glob(path + "*.xml")

    xmls = []
    for xmlfile in files:
        with open(xmlfile, 'r') as f:
            xmls.append(etree.parse(f))

    return xmls


def find_by_block_id(xmls, block_id):
    """
    Find an element by block_id from a list of lxml trees
    """
    block_xpath = etree.XPath("//*[@ID='{id}']".format(id=block_id))
    for xml in xmls:
        elements = block_xpath(xml)
        if elements:
            return elements[0]


some_dir = data_root + 'newspapers/fin/1854/1457-4616/1457-4616_1854-08-01_31/alto/'
xmls = read_xml_directory(some_dir)

print(etree.tostring(find_by_block_id(xmls, 'P2_TB00001')))


