我想撰寫一個可用于不同 XML 檔案(均使用 TEI 編碼)的代碼,以查看特定元素和屬性是否出現,它們出現的頻率以及在什么背景關系中)。為此,我撰寫了以下代碼:
from logging import root
import xml.etree.ElementTree as ET
import csv
f = open('orestes-elements.csv', 'w', encoding="utf-8")
writer = csv.writer(f)
writer.writerow(["Note Attributes", "Note Text", "Responsibility", "Certainty Element", "Certainty Attributes", "Certainty Text"])
tree = ET.parse(r"C:\Users\noahb\OneDrive\Desktop\Humboldt\Semester 2\Daten\Hausarbeit-TEI\edition-euripides\Orestes.xml")
root = tree.getroot()
try:
for note in root.findall('.//note'):
noteat = note.attrib
notetext = note.text
print(noteat)
print(notetext)
#attribute search
for responsibility in root.findall(".//*[@resp]"):
responsibilities = str(responsibility.tag, responsibility.attrib, responsibility.text)
for certainty in root.findall(".//*[@cert]"):
certaintytag = certainty.tag
certaintyat = certainty.attrib
certaintytext = certainty.text
writer.writerow([noteat, notetext, responsibilities, certaintytag, certaintyat, certaintytext])
finally:
f.close()
我收到錯誤“NameError:名稱'noteat'未定義”。我可以縮進 writer.writerrow 但不會添加來自另一個 for 回圈的資訊。如何從不同的 for 回圈中獲取資訊到我的 CSV 檔案中?幫助將不勝感激?(for 回圈中的 print() 為我提供了正確的結果,并且我嘗試將其全部設為一個字串,但這不是必需的,我只是在嘗試不同的解決方案 - 直到現在都沒有作業)。
This is an example of my XML file: (some of the elements and attributes will not appear in some of the files - might this be a reason form the errors?)
<?xml version="1.0" encoding="UTF-8"?>
<!--<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:lang="grc">-->
<?oxygen RNGSchema="teiScholiaSchema2021beta.rng" type="xml"?>
<TEI xml:lang="grc">
<teiHeader>
<titleStmt>
<title cert="high">Scholia on Euripides’ Orestes 1–500</title>
<author><note>Donald J.</note> Mastronarde</author>
</titleStmt>
</teiHeader>
<text>
<div1 type="subdivisionByPlay" xml:id="Orestes">
<div2 type="hypotheseis" xml:id="hypOrestes">
<head type="outer" xml:lang="en">Prefatory material (argumenta/hypotheseis) for Orestes</head>
<p>Orestes, pursuing <note cert="low">(vengeance for)</note> the murder of his father, killed Aegisthus and
Clytemnestra. Having dared to commit matricide he paid the penalty immediately, becoming
mad. And after Tyndareus, the father of the murdered woman, brought an accusation, the
Argives were about to issue a public vote about him, concerning what the man who had acted
impiously should suffer.
</p>
</div2>
</div1>
</text>
</TEI>
Example of what CSV should look like:

uj5u.com熱心網友回復:
writer.writerow()如果缺少元素,則不會定義您的值。您可以定義一些默認值來避免這種情況。
try嘗試在陳述句后添加以下內容:
noteat, notetext, responsibilities, certaintytag, certaintyat, certaintytext = [''] * 6
如果愿意,您當然可以擁有'NA'。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/445729.html
標籤:python xml csv parsing elementtree
上一篇:如何合并CSV檔案,以便將具有唯一識別符號的行添加到輸出的同一行中?
下一篇:將值附加到CSV中的新列
