簡體   English   中英

如何在Unix中的XML中刪除回車符

[英]how to remove carriage returns within xml in unix

我有一個要使用python處理的xml文件

我收到錯誤,因為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"

您可以刪除回車符並繼續進行XML漂亮打印

可以使用xml.dom.minidom.parseString(s).toprettyxml()實現XML漂亮打印

刪除回車符和換行符

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

用xml漂亮打印重新格式化文件

cat xmlfile.txt | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print xml.dom.minidom.parseString(s).toprettyxml()'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM