Skip to content

Instantly share code, notes, and snippets.

@sayanchowdhury
Last active March 19, 2024 17:40
Show Gist options
  • Select an option

  • Save sayanchowdhury/086de75e1bca4dfaa5187d8b9915efa6 to your computer and use it in GitHub Desktop.

Select an option

Save sayanchowdhury/086de75e1bca4dfaa5187d8b9915efa6 to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
with open("txndoc.xml") as fobj:
data = fobj.read()
doc = BeautifulSoup(data, "xml")
responses = doc.find_all("Response")
csv_data = []
for response in responses:
variable = response.find_all("Variable")
if variable:
variable = variable[0].get("name")
else:
variable = ""
answer = response.find_all("Answer")
if answer:
answer = answer[0].text
else:
answer = ""
if len(answer) > 32767:
print(f"Discarding {variable}")
continue
csv_data.append((variable, answer))
import csv
with open('final.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(csv_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment