简体   繁体   English

为什么我的 Python XML 解析器在第一个文件后中断?

[英]Why does my Python XML parser break after the first file?

I am working on a Python (3) XML parser that should extract the text content of specific nodes from every xml file within a folder.我正在研究一个 Python (3) XML 解析器,它应该从文件夹中的每个 xml 文件中提取特定节点的文本内容。 Then, the script should write the collected data into a tab-separated text file.然后,脚本应将收集到的数据写入以制表符分隔的文本文件。 So far, all the functions seem to be working.到目前为止,所有功能似乎都在工作。 The script returns all the information that I want from the first file, but it always breaks, I believe, when it starts to parse the second file.该脚本从第一个文件返回我想要的所有信息,但我相信,当它开始解析第二个文件时,它总是会中断。

When it breaks, it returns "TypeError: 'str' object is not callable."当它中断时,它返回“TypeError: 'str' object is not callable”。 I've checked the second file and found that the functions work just as well on that as the first file when I remove the first file from the folder.我检查了第二个文件,发现当我从文件夹中删除第一个文件时,这些功能与第一个文件一样有效。 I'm very new to Python/XML.我对 Python/XML 很陌生。 Any advice, help, or useful links would be greatly appreciated.任何建议、帮助或有用的链接将不胜感激。 Thanks!谢谢!

import xml.etree.ElementTree as ET
import re
import glob
import csv
import sys

content_file = open('WWP Project/WWP_texts.txt','wt')
quotes_file = open('WWP Project/WWP_quotes.txt', 'wt')
list_of_files = glob.glob("../../../Documents/WWPtextbase/distribution/*.xml")

ns = {'wwp':'http://www.wwp.northeastern.edu/ns/textbase'}

def content(tree):
    lines = ''.join(ET.tostring(tree.getroot(),encoding='unicode',method='text')).replace('\n',' ').replace('\t',' ').strip()
    clean_lines = re.sub(' +',' ', lines)
    return clean_lines.lower()

def quotes(tree):
    quotes_list = []
    for node in tree.findall('.//wwp:quote', namespaces=ns):
        quote = ET.tostring(node,encoding='unicode',method='text')
        clean_quote = re.sub(' +',' ', quote)
        quotes_list.append(clean_quote)
    return ' '.join(str(v) for v in quotes_list).replace('\t','').replace('\n','').lower()

def pid(tree):
    for node in tree.findall('.//wwp:sourceDesc//wwp:author/wwp:persName[1]', namespaces=ns):
        pid = node.attrib.get('ref')
    return pid.replace('personography.xml#','') # will need to replace 'p:'


def trid(tree): # this function will eventually need to call OT (.//wwp:publicationStmt//wwp:idno)
    for node in tree.findall('.//wwp:sourceDesc',namespaces=ns):
        trid = node.attrib.get('n')
    return trid

content_file.write('pid' + '\t' + 'trid' + '\t' +'text' + '\n')
quotes_file.write('pid' + '\t' + 'trid' + '\t' + 'quotes' + '\n')

for file_name in list_of_files:
    file = open(file_name, 'rt')
    tree = ET.parse(file)
    file.close()
    pid = pid(tree)
    trid = trid(tree)
    content = content(tree)
    quotes = quotes(tree)
    content_file.write(pid + '\t' + trid + '\t' + content + '\n')
    quotes_file.write(pid + '\t' + trid + '\t' + quotes + '\n')

content_file.close()
quotes_file.close()

You are overwriting your function calls with the values they returned.您正在使用它们返回的值覆盖您的函数调用。 changing the function names should fix it.更改函数名称应该可以修复它。

import xml.etree.ElementTree as ET
import re
import glob
import csv
import sys

content_file = open('WWP Project/WWP_texts.txt','wt')
quotes_file = open('WWP Project/WWP_quotes.txt', 'wt')
list_of_files = glob.glob("../../../Documents/WWPtextbase/distribution/*.xml")

ns = {'wwp':'http://www.wwp.northeastern.edu/ns/textbase'}

def get_content(tree):
    lines = ''.join(ET.tostring(tree.getroot(),encoding='unicode',method='text')).replace('\n',' ').replace('\t',' ').strip()
    clean_lines = re.sub(' +',' ', lines)
    return clean_lines.lower()

def get_quotes(tree):
    quotes_list = []
    for node in tree.findall('.//wwp:quote', namespaces=ns):
        quote = ET.tostring(node,encoding='unicode',method='text')
        clean_quote = re.sub(' +',' ', quote)
        quotes_list.append(clean_quote)
    return ' '.join(str(v) for v in quotes_list).replace('\t','').replace('\n','').lower()

def get_pid(tree):
    for node in tree.findall('.//wwp:sourceDesc//wwp:author/wwp:persName[1]', namespaces=ns):
        pid = node.attrib.get('ref')
    return pid.replace('personography.xml#','') # will need to replace 'p:'


def get_trid(tree): # this function will eventually need to call OT (.//wwp:publicationStmt//wwp:idno)
    for node in tree.findall('.//wwp:sourceDesc',namespaces=ns):
        trid = node.attrib.get('n')
    return trid

content_file.write('pid' + '\t' + 'trid' + '\t' +'text' + '\n')
quotes_file.write('pid' + '\t' + 'trid' + '\t' + 'quotes' + '\n')

for file_name in list_of_files:
    file = open(file_name, 'rt')
    tree = ET.parse(file)
    file.close()
    pid = get_pid(tree)
    trid = get_trid(tree)
    content = get_content(tree)
    quotes = get_quotes(tree)
    content_file.write(pid + '\t' + trid + '\t' + content + '\n')
    quotes_file.write(pid + '\t' + trid + '\t' + quotes + '\n')

content_file.close()
quotes_file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM