I generate xml content from input file using an xml_gen.bat file (I cannot modify it), and I use it like this on Windows cmd to generate xml:
xml_gen --options [inputname] [optional outputname]
I also have a python script xml_parse.py that parses xml input:
import sys
import lxml.etree as ET
def xml_parse(xml_input):
for event, elem in ET.iterparse(xml_input, events=('end', ), tag='TAG'):
# do something
# write parsed content to file
elem.clear()
def main():
xml_parse(sys.stdin)
if __name__ == '__main__':
main()
Both xml_gen and xml_parse.py work when tested separately, i.e., xm_gen can take an input file and generate a correct xml file; xml_parse.py (ran in PyCharm) can take an input xml file from disk and do the parsing correctly.
Since my actual experiment will generate gigabytes of xml from xml_gen, I want to pipe the output of xml_gen directly to the python script to process them.
When I tried:
dir_to_xml_gen\xml_gen --option dir_to_input\input | python xml_parse.py
It produced such error:
Traceback (most recent call last): File "xml_parse.py", line 77, in main() File "xml_parse.py", line 71, in main xml_parse(sys.stdin) File "xml_parse.py", line 50, in xml_parse for event, elem in ET.iterparse(xml_parse, events=('end', ), tag='TAG'):File "iterparse.pxi", line 208, in lxml.etree.iterparse.next (src\lxml\lxml.etree.c:131498) lxml.etree.XMLSyntaxError: Document is empty, line 2, column 1 ! System error ! 'SPIO_E_END_OF_FILE'
when I tried: dir_to_xml_gen\xml_gen --option dir_to_input\input | python xml_parse.py
it produced similar error: System error 'SPIO_E_END_OF_FILE'
I'm aware of the issues in Cannot redirect output when I run Python script on Windows using just script's name, and followed directions here STDIN/STDOUT Redirection May Not Work If Started from a File Association, but didn't solve my problem. Thank you for your help.