【问题标题】:Python read XML from standard inputPython 从标准输入读取 XML
【发布时间】:2020-10-24 17:15:18
【问题描述】:

我正在尝试从 Python3 中的命令行读取 XML 输入。到目前为止,我尝试了各种方法,以下是我读取 XML 的代码,

import sys
import xml.dom.minidom
try:
    input = sys.stdin.buffer
except AttributeError:
    input = sys.stdin

xmlString = input.read()

但是这种持续获取输入请有人可以告诉如何在获取 XML 文件后停止获取输入

我的 XML 文件是,

<response>
 <article>
  <title>A Novel Approach to Image Classification, in a Cloud Computing Environment stability.</title>
  <publicationtitle>IEEE Transactions on Cloud Computing</publicationtitle>
  <abstract>Classification of items within PDF documents has always been challenging.  This stability document will discuss a simple classification algorithm for indexing images within a PDF.</abstract>
 </article>
 <body>
  <sec>
   <label>I.</label>
   <p>Should Haven't That is a bunch of text pattern these classification and cyrptography.  These paragraphs are nothing but nonsense.  What is the statbility of your program to find neural nets. Throw in some numbers to see if you get the word count correct this is a classification this in my nd and rd words.  What the heck throw in cryptography.</p>
   <p>I bet diseases you can't find probability twice.  Here it is a again probability.  Just to fool you I added it three times probability.  Does this make any pattern classification? pattern classification! pattern classification.</p>
   <p>
    <fig>
     <label>FIGURE.</label>
     <caption>This is a figure representing convolutional neural nets.</caption>
    </fig>
   </p>
 </sec>
 </body>
</response>

由于它有很多行,我无法使用 input() 以传统方式输入它

【问题讨论】:

  • 你为什么要惹sys.stdin?为什么不直接使用input()
  • input() 只占用一行,对吗?我有包含多行的 XML 文件,我需要使用标准输入一次输入

标签: python python-3.x xml


【解决方案1】:

从控制台/命令行读取是使用input() 完成的。试试:

import xml.dom.minidom

xmlString = input()

有关sys.stdin 的更多详细信息,请查看this SO 帖子。

编辑:如果你想从控制台读取多行,试试sys.stdin.readlines,比如xmlString = sys.stdin.readlines()。用户使用 CTRL+D 终止多行输入。或者,您可以让用户将 XML 写入文件,然后解析该文件(更容易,但可能并不理想)。

【讨论】:

  • input() 只占用一行,对吗?我有包含多行的 XML 文件,我需要使用标准输入一次输入