Last active
March 19, 2024 17:40
-
-
Save sayanchowdhury/086de75e1bca4dfaa5187d8b9915efa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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