我现有的工作代码是:
#!/usr/bin/env python3
import glob
import xml.etree.ElementTree as ET
filenames = glob.glob("C:\\Users\\####\\Desktop\\BNC\\[000-ZZZ]*.xml")
out_lines = []
for filename in filenames:
with open(filename, 'r', encoding="utf-8") as content:
tree = ET.parse(content)
root = tree.getroot()
for w in root.iter('w'):
if w.text is None:
w.text = "####"
lemma = w.get('hw')
if lemma is None:
lemma = "####"
pos = w.get('pos')
if pos is None:
pos = "####"
tag = w.get('c5')
if tag is None:
tag = "####"
out_lines.append(w.text + "\t" + lemma + "\t" + pos + "\t" + tag)
with open("C:\\Users\\####\\Desktop\\bnc.txt", "w", encoding="utf-8") as out_file:
for line in out_lines:
out_file.write("{}\n".format(line))
有4,049个xml源文件,这会产生超过2GB的输出,比我可以轻松导入到其他包进行操作的行数多。
我手动批量处理了100个文件,但这仍然导致一些输出文件超过1,048,576行
我希望打印循环以递归方式输出基于设置文件名的文件,在每个1,048,576行之后(或更少,能够指定是理想的)。
e.g。
不知道如何开始这个。