-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def entityParser(self, text: str) -> str: | ||
replace_dict={""":'"',"'":"'","&":"&",">":">","<":"<","⁄":"/"} | ||
res = [] | ||
flag = False | ||
keyword = [] | ||
for c in text: | ||
if c == "&": | ||
if flag: | ||
res.append("".join(keyword)) | ||
flag = True | ||
keyword = ["&"] | ||
elif flag: | ||
keyword.append(c) | ||
if c == ";": | ||
keyword = "".join(keyword) | ||
res.append(replace_dict.get(keyword, keyword)) | ||
flag = False | ||
else: | ||
res.append(c) | ||
if flag: | ||
res.append("".join(keyword)) | ||
return "".join(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
* Solution | ||
其实可以用 replace 的,只要把 ~&~ 另外判断就可以了。 | ||
#+BEGIN_SRC python | ||
class Solution: | ||
def entityParser(self, text: str) -> str: | ||
special = { | ||
""": '"', | ||
"'": "'", | ||
# "&": "&", | ||
">": ">", | ||
"<": "<", | ||
"⁄": "/"} | ||
|
||
for entity, ch in special.items(): | ||
text = text.replace(entity, ch) | ||
text = text.replace("&", "&") | ||
return text | ||
#+END_SRC |