0

I have text as in below in a text file. All in one line

I want to read text between tags and print it nd I want to print it in multiple lines by breaking at '~', like below. How can I do that in python

Line 1- This is the data you requested~
Line 2- Use this data for processing~
Line 3 - If processing fails contact us @ 188-500-5678~


<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><re:EnvelopeResponse xmlns:core="http://www.AAAA.org/SOAP/SDL/Rule21.1.0.xsd"><PayloadType>Response</PayloadType><ProcessingMode>R</ProcessingMode><PayloadID>42eaf653</PayloadID><TimeStamp>2021-06-01</TimeStamp><SenderID>X</SenderID><ReceiverID>X</ReceiverID><RuleVersion>1.1.0</RuleVersion><Payload>This is the data you requested~Use this data for processing~If processing fails contact us @ 188-500-5678~</Payload><ErrorCode>Success</ErrorCode><ErrorMessage/></R:EnvelopeResponse></env:Body></env:Envelope>
John Gordon
  • 29,573
  • 7
  • 33
  • 58
Bobby
  • 109
  • 8

1 Answers1

2

This way you can extract the payload:

from bs4 import BeautifulSoup

data = """<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><re:EnvelopeResponse xmlns:core="http://www.AAAA.org/SOAP/SDL/Rule21.1.0.xsd"><PayloadType>Response</PayloadType><ProcessingMode>R</ProcessingMode><PayloadID>42eaf653</PayloadID><TimeStamp>2021-06-01</TimeStamp><SenderID>X</SenderID><ReceiverID>X</ReceiverID><RuleVersion>1.1.0</RuleVersion><Payload>This is the data you requested~Use this data for processing~If processing fails contact us @ 188-500-5678~</Payload><ErrorCode>Success</ErrorCode><ErrorMessage/></R:EnvelopeResponse></env:Body></env:Envelope>"""
soup = BeautifulSoup(data, "xml")
items = soup.find_all("Payload")
print (items)

You can use \n to set newline. You can do that with replace(). You can also use replace() to remove the tags in the string.

DSteman
  • 1,388
  • 2
  • 12
  • 25