Skip to content

Instantly share code, notes, and snippets.

@shikajiro
Created February 7, 2024 07:41
Show Gist options
  • Save shikajiro/652de8628adf81512cb99560e01b5080 to your computer and use it in GitHub Desktop.
Save shikajiro/652de8628adf81512cb99560e01b5080 to your computer and use it in GitHub Desktop.
pepupからダウンロードした医療費のxmlをcsvに変換するスクリプト
import xml.etree.ElementTree as ET
import csv
tree = ET.parse('pepup_download.xml')
root = tree.getroot()
csv_data = []
for child in root:
if child.tag != '{http://xml.e-tax.nta.go.jp/XSD/kyotsu}WBD00000':
continue
for element in child:
if element.tag != "{http://xml.e-tax.nta.go.jp/XSD/kyotsu}WBD00010":
continue
row = []
for item in element:
if item.tag == "{http://xml.e-tax.nta.go.jp/XSD/kyotsu}WBD00030":
y = item.find("{http://xml.e-tax.nta.go.jp/XSD/general}yyyy").text
m = item.find("{http://xml.e-tax.nta.go.jp/XSD/general}mm").text
row.append(f"{y}/{int(m):02}")
else:
row.append(item.text)
csv_data.append(row)
with open('output.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