Python 使用unix删除嵌入在xml文本中的回车符

Python 使用unix删除嵌入在xml文本中的回车符,python,xml,unix,Python,Xml,Unix,我有一个xml文件,我正试图用python处理它 我得到了一些错误,因为偶尔xml字符串中的一些文本会在其中强制回车 在unix中,如何在xml文本中删除这些回车,而不删除所有回车,因为这意味着将所有xml记录连接在一起 我可以解析的xml脚本示例: <?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute= 'hello world,

我有一个xml文件,我正试图用python处理它

我得到了一些错误,因为偶尔xml字符串中的一些文本会在其中强制回车

在unix中,如何在xml文本中删除这些回车,而不删除所有回车,因为这意味着将所有xml记录连接在一起

我可以解析的xml脚本示例:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute= 'hello world, i am not going to add a cariage return right now'></message></script>

由于回车而无法解析的xml脚本示例:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz">
<message attribute= 'hello world, i am going to add a cariage return
right now
even though
i do not have to'></message></script>

解析后的最终输出如下所示:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute = 'hello world, i am not going to add a cariage return right now'></message></script>
<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute= 'hello world, i am going to add a cariage return right now even though i do not have to'></message></script>

我不想删除所有回车,因为我的最终输出如下所示:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute= 'hello world, i am not going to add a cariage return right now'></message></script><?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz"><message attribute = 'hello world, i am going to add a cariage return right now even though i do not have to'></message></script>

首先,示例不是有效的xml。可能是这样的:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz">
<message attribute = 'hello world, i am going to add a cariage return
right now
even though
i do not have to'/></script>

首先,该示例不是有效的xml。可能是这样的:

<?xml version="1.0"?><script startAt="2015-03-25T20:59:38Z" sessionId="xyz">
<message attribute = 'hello world, i am going to add a cariage return
right now
even though
i do not have to'/></script>

在打开xml文件时,您可能还可以使用python对的支持。这将使python用
\n
替换任何
\r\n
\r

要使用它,只需将
U
添加到:


在打开xml文件时,您可能还可以使用python对的支持。这将使python用
\n
替换任何
\r\n
\r

要使用它,只需将
U
添加到:


使用
tr-d'\n'
Remove newlines with
tr-d'\n'
删除换行符我想删除\n但是当我在记事本++中显示xml数据时,\n没有显示为\n,而是显示为CRLF当您使用python打开文件时,CRLF被转换为LF。无论如何,我编辑了代码以使用正则表达式删除CRLF或LF。“当您使用python打开文件时,CRLF将转换为LF。”False。只有在您指定通用换行模式(如@sebastian在其回答中提到的)时才会发生这种情况。根据他的回答,默认情况下会启用该模式。根据我自己的经验,我也可以说它在默认情况下是启用的,但我在使用Windows的机器上工作。在Linux/MAC上是否有所不同?我想删除\n但当我在notepad++中显示xml数据时,\n不是显示为\n,而是显示为CRLF当您使用python打开文件时,CRLF被转换为LF。无论如何,我编辑了代码以使用正则表达式删除CRLF或LF。“当您使用python打开文件时,CRLF将转换为LF。”False。只有在您指定通用换行模式(如@sebastian在其回答中提到的)时才会发生这种情况。根据他的回答,默认情况下会启用该模式。根据我自己的经验,我也可以说它在默认情况下是启用的,但我在使用Windows的机器上工作。在Linux/MAC上有什么不同吗?
import re
from lxml import etree

def removeEndl(xml):
   root = etree.XML(xml)

   for element in root.xpath('//*'):
      if element.text is not None:
         element.text = re.sub(r'\r?\n', '', element.text)
      for key, value in element.attrib.iteritems():
         element.attrib[key] = re.sub(r'\r?\n', '', value)

   return etree.tostring(root)
import elementtree.ElementTree as ET
with open('my.xml', 'rU') as myxml:
    ET.parse(myxml)