-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_reader.py
More file actions
79 lines (72 loc) · 2.1 KB
/
Copy pathxml_reader.py
File metadata and controls
79 lines (72 loc) · 2.1 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sqlalchemy as sq
import urllib.parse
import numpy as np
import pandas as pd
import xml.etree.ElementTree as ET
def read_x():
#get source file in Tree Structure
tree = ET.parse('test.xml')
#get root of XML tree
root = tree.getroot()
my_dic = {}
l = tuple
print(f"{root.tag} {root.attrib}")
for child in root.iter('movie'):
print(f"Tag: {child.tag} Attrib: {child.attrib}")
my_dic.update(child.attrib)
for movie in root.findall("./genre/decade/movie/[year='1992']"):
print("xpath")
print(movie.attrib)
#retrive tags from XML
def get_txt():
tree = ET.parse('test.xml')
root = tree.getroot()
list = []
dict = {}
for child in root.iter():
print(child.findtext('Series_ID'))
if child.findtext('Series_ID') !=None:
list.append(child.findtext('Series_ID'))
for series in list:
print(series)
#for x in root.iter():
#print(x.tag)
#for index ,k, v in enumerate(root.items()):
# print(f'{k} {v} index {index}')
#for element in root.iter():
# print(f"Tag:{element.tag},Text: {element.text},Attr {element.attrib}\n")
def xml_dt():
tree = ET.parse('CPI_Data.xml')
xm_df = pd.read_xml('CPI_Data.xml')
df = pd.DataFrame(data=xm_df, columns=['Series_ID','Dt','Value','Per_Change'])
print(df.head(10))
def read_fl():
#reading in regular file
lines = []
with open('exprt.xml', 'r') as f:
for x in f:
lines.append(x.strip())
print(x.strip())
def parse_element(elem, item):
if len(list(elem))==0:
item[elem.tag]=elem.text
else:
for child in list(elem):
parse_element(child, item)
tree = ET.parse('test.xml')
root = tree.getroot()
#make array to store dictionary xml results
data= []
for child in root:
#dictionary appended to array created abobe.
item={}
parse_element(child, item)
data.append(item)
for i, x in enumerate(data):
if i<5:
i+=1
print(x)
else:
break
df = pd.DataFrame(data=data)
#print(df)