如何在unix

时间:2016-03-29 23:01:35

标签: python xml unix

我有一个xml文件,我正在尝试使用python

进行处理

我收到错误,因为有时xml字符串中的某些文本强制在其中返回回车

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

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

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

解析后的最终输出结果如下:

script_dt, session_id, message
2015-03-25T20:59:38Z,"xyz","hello world, i am not going to add a cariage return right now"
2015-03-25T20:59:38Z,"xyz","hello world,i am going to add a cariage return right now even though i do not have to"

2 个答案:

答案 0 :(得分:2)

您可以删除回车和&amp;继续进行XML漂亮打印

使用可以实现XML漂亮的打印 xml.dom.minidom.parseString(s).toprettyxml()

删除回车&amp;新行字符

perl -i -pe 's/\r\n//' xmlfile.txt 

使用xml pretty print

重新格式化文件
cat xmlfile.txt | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'

答案 1 :(得分:-1)

Trim和sed命令可以用于相同的。

tr -d&#39; \ r&#39; filewithoutcarriagereturns

sed -i s / \ r //