替换字符串中的字符并在 python 中返回
Replacing characters in a string and returning in python
我正在使用 Pycharm 作为在 python 中编码的软件工具。
这些词采用文本格式,但它们应该return 不同的输出
word = "<p>Santa is fat</p>"
secondword = "Potato & Tomato"
thirdword = "Koala eats http://koala.org/ a lot</p>"
我想将以下每个“<”、“>”、“&”替换为“<
”、“>
” , "&
"
所以输出应该是这样的
outputword = "<p>Santa is fat</p>"
outputsecondword = "Fish & Chips"
outputthirdword = ""<p>Koala eats <a href='http://koala.org/'>http://koala.org/</a> a lot</p>"
注意第三个单词是 URL。
我不想使用 html 库。
我是 Python 的菜鸟,所以请为我提供简单的解决方案。我考虑过使用列表,但每当我替换列表中的字符时,它都不会改变
Python 来 with batteries included:
import html
word = "<p>Santa is fat</p>"
print(html.escape(word))
输出:
<p>Santa is fat</p>
不使用 html
库,您可以像这样进行替换:
replacewith = {'<':'lt;', '>':'gt;'}
for w in replacewith:
word = word.replace(w,replacewith[w])
In [407]: word
Out[407]: 'lt;pgt;Santa is fatlt;/pgt;'
或者,在一行中:
word.replace('<','lt;').replace('>','gt;')
更新:
您可以将代码移动到函数中并像这样调用它:
def replace_char(word, replacewith=replacewith):
for w in replacewith:
word = word.replace(w,replacewith[w])
return word
像下面这样用 word
调用它会给你:
replace_char("<p>Santa is fat</p>")
Out[457]: 'lt;pgt;Santa is fatlt;/pgt;'
要使第二个工作正常,请更新字典:
In [454]: replacewith.update({'Potato':'Fish', 'Tomato':'Chips', '&': '&',})
In [455]: replace_char("Potato & Tomato", replacewith)
Out[455]: 'Fish & Chips'
您可以以几乎相同的方式对可能出现在其他新字符串中的任何新字符执行相同的操作。您输入的 thirdword
开头缺少 <p>
。
In [461]: replacewith.update({'http://koala.org/':'<a href="http://koala.org/">http://koala.org/</a>'})
In [463]: replace_char("Koala eats http://koala.org/ a lot</p>", replacewith)
Out[463]: 'Koala eats lt;a href="http://koala.org/"gt;http://koala.org/lt;/agt; a lotlt;/pgt;'
我正在使用 Pycharm 作为在 python 中编码的软件工具。
这些词采用文本格式,但它们应该return 不同的输出
word = "<p>Santa is fat</p>"
secondword = "Potato & Tomato"
thirdword = "Koala eats http://koala.org/ a lot</p>"
我想将以下每个“<”、“>”、“&”替换为“<
”、“>
” , "&
"
所以输出应该是这样的
outputword = "<p>Santa is fat</p>"
outputsecondword = "Fish & Chips"
outputthirdword = ""<p>Koala eats <a href='http://koala.org/'>http://koala.org/</a> a lot</p>"
注意第三个单词是 URL。 我不想使用 html 库。 我是 Python 的菜鸟,所以请为我提供简单的解决方案。我考虑过使用列表,但每当我替换列表中的字符时,它都不会改变
Python 来 with batteries included:
import html
word = "<p>Santa is fat</p>"
print(html.escape(word))
输出:
<p>Santa is fat</p>
不使用 html
库,您可以像这样进行替换:
replacewith = {'<':'lt;', '>':'gt;'}
for w in replacewith:
word = word.replace(w,replacewith[w])
In [407]: word
Out[407]: 'lt;pgt;Santa is fatlt;/pgt;'
或者,在一行中:
word.replace('<','lt;').replace('>','gt;')
更新:
您可以将代码移动到函数中并像这样调用它:
def replace_char(word, replacewith=replacewith):
for w in replacewith:
word = word.replace(w,replacewith[w])
return word
像下面这样用 word
调用它会给你:
replace_char("<p>Santa is fat</p>")
Out[457]: 'lt;pgt;Santa is fatlt;/pgt;'
要使第二个工作正常,请更新字典:
In [454]: replacewith.update({'Potato':'Fish', 'Tomato':'Chips', '&': '&',})
In [455]: replace_char("Potato & Tomato", replacewith)
Out[455]: 'Fish & Chips'
您可以以几乎相同的方式对可能出现在其他新字符串中的任何新字符执行相同的操作。您输入的 thirdword
开头缺少 <p>
。
In [461]: replacewith.update({'http://koala.org/':'<a href="http://koala.org/">http://koala.org/</a>'})
In [463]: replace_char("Koala eats http://koala.org/ a lot</p>", replacewith)
Out[463]: 'Koala eats lt;a href="http://koala.org/"gt;http://koala.org/lt;/agt; a lotlt;/pgt;'