forked from LinkedInLearning/learning-python-2896241
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadXMLFile.py
More file actions
43 lines (25 loc) · 1.42 KB
/
Copy pathLoadXMLFile.py
File metadata and controls
43 lines (25 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Not needed unless running locally
# !wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Sample-employee-XML-file.xml
import xml.etree.ElementTree as etree
import pandas as pd
filename = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Sample-employee-XML-file.xml"
async def download(url, filename):
response = await pyfetch(url)
if response.status == 200:
with open(filename, "wb") as f:
f.write(await response.bytes())
await download(filename, "Sample-employee-XML-file.xml")
tree = etree.parse("Sample-employee-XML-file.xml")
root = tree.getroot()
columns = ["firstname", "lastname", "title", "division", "building","room"]
datatframe = pd.DataFrame(columns = columns)
for node in root:
firstname = node.find("firstname").text
lastname = node.find("lastname").text
title = node.find("title").text
division = node.find("division").text
building = node.find("building").text
room = node.find("room").text
datatframe = pd.concat([datatframe, pd.Series([firstname, lastname, title, division, building, room], index = columns)], ignore_index = True)
df=pd.read_xml("Sample-employee-XML-file.xml", xpath="/employees/details")
datatframe.to_csv("employee.csv", index=False)