python xml字符串_python -解析字符串,并返回xml格式字符串 急该如何解决
python --解析字符串,并返回xml格式字符串 急急急。
str?=?"""Registrations:
=================================================================================================
Call-ID:????????8945da7a7f550c16NWRjZjdmMjhmNWQxYTZlOTJjMDY5YjhiN2RjMDViMWE.
User:???????????1000@192.168.1.111
Contact:????????"1000"?<1000>1000>
Agent:??????????X-Lite?release?1002tx?stamp?29712
Status:?????????Registered(UDP)(unknown)?EXP(2014-07-23?10:19:00)?EXPSECS(2458)
Host:???????????debian
IP:?????????????192.168.1.109
Port:???????????35838
Auth-User:??????1000
Auth-Realm:?????192.168.1.111
MWI-Account:????1000@192.168.1.111
Total?items?returned:?1
================================================================================================="""
這個字符串如何解析成列表,并構造成xml格式字符串。請各位大神注意,這個字符串contact字段中的值有冒號,
------解決方案--------------------
#?-*-?coding:?cp936?-*-
str?=?"""Registrations:
=================================================================================================
Call-ID:????????8945da7a7f550c16NWRjZjdmMjhmNWQxYTZlOTJjMDY5YjhiN2RjMDViMWE.
User:???????????1000@192.168.1.111
Contact:????????"1000"?<1000>1000>
Agent:??????????X-Lite?release?1002tx?stamp?29712
Status:?????????Registered(UDP)(unknown)?EXP(2014-07-23?10:19:00)?EXPSECS(2458)
Host:???????????debian
IP:?????????????192.168.1.109
Port:???????????35838
Auth-User:??????1000
Auth-Realm:?????192.168.1.111
MWI-Account:????1000@192.168.1.111
Total?items?returned:?1
================================================================================================="""
lineList?=?str.split("\n")
dictA?=?{}
listB?=?[]
for?line?in?lineList:
curLine?=?line.strip().split(":",1)
if?len(curLine)?!=2:
continue
key?=?curLine[0].strip()
value?=?curLine[1].strip()
if?value?==?"":
continue
dictA[key]?=?value?#你確定你不要字典?
listB.append((key,value))
print?dictA
print?listB
------解決方案--------------------
import?re
from?lxml?import?etree
s?=?"""Registrations:
=================================================================================================
Call-ID:????????8945da7a7f550c16NWRjZjdmMjhmNWQxYTZlOTJjMDY5YjhiN2RjMDViMWE.
User:???????????1000@192.168.1.111
Contact:????????"1000"?<1000>1000>
Agent:??????????X-Lite?release?1002tx?stamp?29712
Status:?????????Registered(UDP)(unknown)?EXP(2014-07-23?10:19:00)?EXPSECS(2458)
Host:???????????debian
IP:?????????????192.168.1.109
Port:???????????35838
Auth-User:??????1000
Auth-Realm:?????192.168.1.111
MWI-Account:????1000@192.168.1.111
Total?items?returned:?1
================================================================================================="""
root?=?etree.Element('root')
data?=?s.split('\n')
for?l?in?data[:-2]:
m?=?re.match('^([^:]+):\s+(.*)',?l)
if?m:
id,?info?=?m.groups()
e?=?etree.Element(id)
e.text?=?info
root.append(e)
print?etree.tostring(root,?pretty_print=True)
記得先安裝下?lxml,windows?下可以直接到官網下載安裝包;linux?下可以?pip?install?lxml
總結
以上是生活随笔為你收集整理的python xml字符串_python -解析字符串,并返回xml格式字符串 急该如何解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三维数据平滑处理_黑白象片的密度信息 卫
- 下一篇: python创建dataframe表格不