我有一个20gbbz2xml文件。格式如下:
text text text ....
我需要以以下格式将其处理为tsv文件:
^{pr2}$
在python和java中最有效的方法是什么?它们之间有什么区别(内存效率和速度方面)。基本上我想这样做:read bz2 file
read the xml file element by element
for each element
retrieve id, url, title and text
print_to_file(idurltitleprocess(text))
感谢您提前回答。在
更新1(基于@Andreas的建议):XMLInputFactory factory = XMLInputFactory.newFactory();
XMLStreamReader xmlReader = factory.createXMLStreamReader(in);
xmlReader.nextTag();
if (! xmlReader.getLocalName().equals("doc")) {
xmlReader.nextTag(); }
String id = xmlReader.getAttributeValue(null, "id");
String url = xmlReader.getAttributeValue(null, "url");
String title = xmlReader.getAttributeValue(null, "title");
String content = xmlReader.getElementText();
out.println(id + '\t' + content);
问题是我只得到第一个元素。在
UPDATE2(我最后用regex来做):if (str.startsWith("
id = str.split("id")[1].substring(2).split("\"")[0];
url = str.split("url")[1].substring(2).split("\"")[0];
title = str.split("title")[1].substring(2).split("\"")[0];
}
else if (str.startsWith("
out.println(uniq_id + '\t' + contect);
content ="";
}
else {
content = content + " " + str;
}