-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1410.html-实体解析器.py
39 lines (35 loc) · 1.26 KB
/
1410.html-实体解析器.py
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
#
# @lc app=leetcode.cn id=1410 lang=python
#
# [1410] HTML 实体解析器
#
# @lc code=start
class Solution(object):
def entityParser(self, text):
"""
:type text: str
:rtype: str
"""
i = 0
while i < len(text):
if text[i] == '&':
end_position = i
while end_position < len(text) and text[end_position] != ';':
end_position += 1
if end_position < len(text):
entity = text[i + 1:end_position]
if entity == 'amp':
text = text[:i] + '&' + text[end_position + 1:]
elif entity == 'lt':
text = text[:i] + '<' + text[end_position + 1:]
elif entity == 'gt':
text = text[:i] + '>' + text[end_position + 1:]
elif entity == 'quot':
text = text[:i] + '\"' + text[end_position + 1:]
elif entity == 'apos':
text = text[:i] + '\'' + text[end_position + 1:]
elif entity == "frasl":
text = text[:i] + '/' + text[end_position + 1:]
i += 1
return text
# @lc code=end