Skip to content

Commit fdffd7e

Browse files
committed
C1
1 parent e229016 commit fdffd7e

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

XML Prasing.ipynb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Name: Chuck\n",
13+
"Attr: yes\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"import xml.etree.ElementTree as ET\n",
19+
"data = '''\n",
20+
"<person>\n",
21+
"\n",
22+
" <name>Chuck</name>\n",
23+
" <phone type = \"intl\">\n",
24+
" 0779395415\n",
25+
" </phone>\n",
26+
" <email hide=\"yes\"/>\n",
27+
"</person>\n",
28+
"'''\n",
29+
"\n",
30+
"tree = ET.fromstring(data)\n",
31+
"print('Name:', tree.find('name').text)\n",
32+
"print('Attr:', tree.find('email').get('hide'))"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 5,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"ename": "ParseError",
42+
"evalue": "mismatched tag: line 13, column 2 (<string>)",
43+
"output_type": "error",
44+
"traceback": [
45+
"Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n",
46+
" File \u001b[0;32m\"C:\\Users\\hmroh\\anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py\"\u001b[0m, line \u001b[0;32m3343\u001b[0m, in \u001b[0;35mrun_code\u001b[0m\n exec(code_obj, self.user_global_ns, self.user_ns)\n",
47+
" File \u001b[0;32m\"<ipython-input-5-7ec3009b2c20>\"\u001b[0m, line \u001b[0;32m16\u001b[0m, in \u001b[0;35m<module>\u001b[0m\n stuff = ET.fromstring(input)\n",
48+
"\u001b[1;36m File \u001b[1;32m\"C:\\Users\\hmroh\\anaconda3\\lib\\xml\\etree\\ElementTree.py\"\u001b[1;36m, line \u001b[1;32m1320\u001b[1;36m, in \u001b[1;35mXML\u001b[1;36m\u001b[0m\n\u001b[1;33m parser.feed(text)\u001b[0m\n",
49+
"\u001b[1;36m File \u001b[1;32m\"<string>\"\u001b[1;36m, line \u001b[1;32munknown\u001b[0m\n\u001b[1;31mParseError\u001b[0m\u001b[1;31m:\u001b[0m mismatched tag: line 13, column 2\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"input = '''\n",
55+
"<stuff>\n",
56+
" <users>\n",
57+
" <user x=\"2\">\n",
58+
" <id>001</id>\n",
59+
" <name>Chuck</name>\n",
60+
" </user>\n",
61+
" <user x=\"7\">\n",
62+
" <id>009</id>\n",
63+
" <name>Brent</name>\n",
64+
" </user>\n",
65+
" <users>\n",
66+
"</stuff>\n",
67+
"'''\n",
68+
"\n",
69+
"stuff = ET.fromstring(input)\n",
70+
"lst = stuff.findall('users/user')\n",
71+
"print('User count:', len(lst))\n",
72+
"\n",
73+
"for item in lst:\n",
74+
" print('Name', item.find('name').text)\n",
75+
" print('Id', item.find('id').text)\n"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
}
85+
],
86+
"metadata": {
87+
"kernelspec": {
88+
"display_name": "Python 3",
89+
"language": "python",
90+
"name": "python3"
91+
},
92+
"language_info": {
93+
"codemirror_mode": {
94+
"name": "ipython",
95+
"version": 3
96+
},
97+
"file_extension": ".py",
98+
"mimetype": "text/x-python",
99+
"name": "python",
100+
"nbconvert_exporter": "python",
101+
"pygments_lexer": "ipython3",
102+
"version": "3.8.3"
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 4
107+
}

0 commit comments

Comments
 (0)