如何替换字符串中的™?
How to replace ™ in a string?
我有一个字符串如下所示:Getting Started with NetX™ DHCP rev1.05
我想用 %E2%84%A2 替换 TM。
我添加了:# -- coding: utf-8 -- 到文件的最顶部,还是不行,没有弹出错误
我正在使用 Python 2.7
这是我的 python 代码:
def create_link(title):
temp_title = title.replace(' ', '%20') # first replace space with %20. works fine
temp_title.replace('™', '%E2%84%A2') # then replace TM, not working
link = 'https://ApplicationNotes/'+ temp_title
return link
我想你使用 re
模块:
import re
def create_link(title):
temp_title = title.replace(' ', '%20') # first replace space with %20. works fine
temp_title = re.sub(r'™', r'%E2%84%A2', temp_title) # this change
link = 'https://ApplicationNotes/'+ temp_title
return link
替换不起作用,因为对于 str.replace()
的第二次调用,return 值没有分配给任何东西,所以它丢失了。您可以使用以下方法修复它:
temp_title = temp_title.replace('™', '%E2%84%A2')
将 return 值绑定到 temp_title
,但是,请考虑以下事项。
因为你想对字符串进行百分比编码以便在 URL 中使用,你可以简单地使用 urlib.quote()
:
>>> title = 'NetX™ DHCP rev1.05'
>>> title
'NetX\xe2\x84\xa2 DHCP rev1.05'
>>> import urllib # Python 2
>>> urllib.quote(title)
'NetX%E2%84%A2%20DHCP%20rev1.05'
您会注意到空格也已为您处理。所以你可以这样写你的函数:
def create_link(title):
return urllib.quote('https://ApplicationNotes/{}'.format(title))
它的优点是还可以对 URL 中其他符合条件的字符进行百分比编码。
为了完整起见,如果您使用的是 Python 3:
>>> from urllib.parse import quote
>>> quote('NetX™ DHCP rev1.05')
'NetX%E2%84%A2%20DHCP%20rev1.05'
您甚至可能不需要引用 URL,具体取决于您要使用它做什么。如果您使用 requests
发送对 URL 的 HTTP 请求,您可以按原样使用它:
>>> import requests
>>> r = requests.get('https://ApplicationNotes/NetX™ DHCP rev1.05')
>>> r.url
u'https://ApplicationNotes/NetX%E2%84%A2%20DHCP%20rev1.05'
我使用的是 python 3.4,这段代码对我有用。请将第 3 行更改为 temp_title = temp_title.replace('™', '%E2%84%A2')
def create_link(title):
temp_title = title.replace(' ', '%20')
temp_title = temp_title.replace('™', '%E2%84%A2')
link = 'https://ApplicationNotes/'+ temp_title
return link
我有一个字符串如下所示:Getting Started with NetX™ DHCP rev1.05
我想用 %E2%84%A2 替换 TM。
我添加了:# -- coding: utf-8 -- 到文件的最顶部,还是不行,没有弹出错误
我正在使用 Python 2.7
这是我的 python 代码:
def create_link(title):
temp_title = title.replace(' ', '%20') # first replace space with %20. works fine
temp_title.replace('™', '%E2%84%A2') # then replace TM, not working
link = 'https://ApplicationNotes/'+ temp_title
return link
我想你使用 re
模块:
import re
def create_link(title):
temp_title = title.replace(' ', '%20') # first replace space with %20. works fine
temp_title = re.sub(r'™', r'%E2%84%A2', temp_title) # this change
link = 'https://ApplicationNotes/'+ temp_title
return link
替换不起作用,因为对于 str.replace()
的第二次调用,return 值没有分配给任何东西,所以它丢失了。您可以使用以下方法修复它:
temp_title = temp_title.replace('™', '%E2%84%A2')
将 return 值绑定到 temp_title
,但是,请考虑以下事项。
因为你想对字符串进行百分比编码以便在 URL 中使用,你可以简单地使用 urlib.quote()
:
>>> title = 'NetX™ DHCP rev1.05'
>>> title
'NetX\xe2\x84\xa2 DHCP rev1.05'
>>> import urllib # Python 2
>>> urllib.quote(title)
'NetX%E2%84%A2%20DHCP%20rev1.05'
您会注意到空格也已为您处理。所以你可以这样写你的函数:
def create_link(title):
return urllib.quote('https://ApplicationNotes/{}'.format(title))
它的优点是还可以对 URL 中其他符合条件的字符进行百分比编码。
为了完整起见,如果您使用的是 Python 3:
>>> from urllib.parse import quote
>>> quote('NetX™ DHCP rev1.05')
'NetX%E2%84%A2%20DHCP%20rev1.05'
您甚至可能不需要引用 URL,具体取决于您要使用它做什么。如果您使用 requests
发送对 URL 的 HTTP 请求,您可以按原样使用它:
>>> import requests
>>> r = requests.get('https://ApplicationNotes/NetX™ DHCP rev1.05')
>>> r.url
u'https://ApplicationNotes/NetX%E2%84%A2%20DHCP%20rev1.05'
我使用的是 python 3.4,这段代码对我有用。请将第 3 行更改为 temp_title = temp_title.replace('™', '%E2%84%A2')
def create_link(title):
temp_title = title.replace(' ', '%20')
temp_title = temp_title.replace('™', '%E2%84%A2')
link = 'https://ApplicationNotes/'+ temp_title
return link