-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathread.py
35 lines (28 loc) · 987 Bytes
/
read.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from glob import glob
from lxml.etree import parse
from MyCapytain.common.constants import XPATH_NAMESPACES
files = glob("sources/*/*.xml")
data = ["\t".join(["File", "Author", "Title"])]
for file in files:
try:
with open(file) as f:
xml = parse(f)
except Exception as E:
print(file+" is failing")
raise E
title = xml.xpath("//tei:titleStmt/tei:title", namespaces=XPATH_NAMESPACES)
author = xml.xpath("//tei:titleStmt/tei:author", namespaces=XPATH_NAMESPACES)
description = xml.xpath("//tei:sourceDesc", namespaces=XPATH_NAMESPACES)
if len(title) == 0:
raise Exception("There is not title for %s" % file)
else:
title = title[0].text
if len(author) == 0:
author = "Anonyme"
else:
author = author[0].text
if author is None:
author = "Anonyme"
data.append("\t".join([file, author, title]))
with open("sources.csv", "w") as f:
f.write("\n".join(data))