How to replace ™ in a string?

Jennifer Zou

I have a string looks like this: Getting Started with NetX™ DHCP rev1.05

I want to replace TM with %E2%84%A2.

I added:# -- coding: utf-8 -- to the very top of the file, still not work, no errors pop up

I am using Python 2.7

Here's my python code:

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
mhawke

The replacement does not work because for the second call to str.replace() the return value is not assigned to anything, so it is lost. You could fix it with:

temp_title = temp_title.replace('™', '%E2%84%A2')

to bind the return value to temp_title, however, consider the following.

Since you want to percent encode the string for use in a URL you can simply use 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'

You'll notice that the spaces have also been handled for you. So you could write your function like this:

def create_link(title):
    return urllib.quote('https://ApplicationNotes/{}'.format(title))

which has the advantage of also percent encoding other eligible characters in the URL.

For completeness, if you were using Python 3:

>>> from urllib.parse import quote
>>> quote('NetX™ DHCP rev1.05')
'NetX%E2%84%A2%20DHCP%20rev1.05'

You might not even need to quote URL depending on what you want to do with it. If you are using requests to send a HTTP request for the URL you can just use it as is:

>>> import requests
>>> r = requests.get('https://ApplicationNotes/NetX™ DHCP rev1.05')
>>> r.url
u'https://ApplicationNotes/NetX%E2%84%A2%20DHCP%20rev1.05'

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to replace string in "%" in make file

分類Dev

How to replace string and upload with php

分類Dev

How to split String with "\" as a delimiter? and also how to replace "\" into ":"?

分類Dev

I am confuse how to replace string preg replace

分類Dev

How to sed and replace string with a folder path

分類Dev

How to replace part of a string using regex

分類Dev

How to replace a character in a string using nunjucks

分類Dev

How do I replace each substring in a string?

分類Dev

How to replace a number within a string by itself + 1?

分類Dev

How to insert a tag in a string to replace a letter and to customize it?

分類Dev

how to replace number in a string that contains brackets in php

分類Dev

How to recusively replace string by regex in Java

分類Dev

How to tie a string replace to a command in Light Table

分類Dev

How to use string in JS replace() parameter

分類Dev

replace `_` with `-` in a string

分類Dev

How to replace a sub-string in a larger-string that matches to an egrep?

分類Dev

How can I replace the first occurence of a sub-string in a string?

分類Dev

How to replace a \string with a non-backslashed string in emacs abbrev mode?

分類Dev

How to search and replace a string while preserving variable part of the string in vim

分類Dev

How do I replace a string value with a NULL in PySpark?

分類Dev

How to convert list to string in Groovy and remove brackets without using replace?

分類Dev

How to replace all roman numbers in a string for the arabic equivalent?

分類Dev

How to replace the last char of a string in bash with another char

分類Dev

How to replace all occurrences of a string except the first one in JavaScript?

分類Dev

How to replace a string between double brackets while typing?

分類Dev

How to replace a single slash '/' in a string excluding the slash characters in the '://' pattern

分類Dev

How to find and replace a string with PowerShell - issue with Eastern European characters

分類Dev

How to strip or replace all matches of a pattern from a string in Bash?

分類Dev

How to replace random parts of string with random keys from array?

Related 関連記事

  1. 1

    How to replace string in "%" in make file

  2. 2

    How to replace string and upload with php

  3. 3

    How to split String with "\" as a delimiter? and also how to replace "\" into ":"?

  4. 4

    I am confuse how to replace string preg replace

  5. 5

    How to sed and replace string with a folder path

  6. 6

    How to replace part of a string using regex

  7. 7

    How to replace a character in a string using nunjucks

  8. 8

    How do I replace each substring in a string?

  9. 9

    How to replace a number within a string by itself + 1?

  10. 10

    How to insert a tag in a string to replace a letter and to customize it?

  11. 11

    how to replace number in a string that contains brackets in php

  12. 12

    How to recusively replace string by regex in Java

  13. 13

    How to tie a string replace to a command in Light Table

  14. 14

    How to use string in JS replace() parameter

  15. 15

    replace `_` with `-` in a string

  16. 16

    How to replace a sub-string in a larger-string that matches to an egrep?

  17. 17

    How can I replace the first occurence of a sub-string in a string?

  18. 18

    How to replace a \string with a non-backslashed string in emacs abbrev mode?

  19. 19

    How to search and replace a string while preserving variable part of the string in vim

  20. 20

    How do I replace a string value with a NULL in PySpark?

  21. 21

    How to convert list to string in Groovy and remove brackets without using replace?

  22. 22

    How to replace all roman numbers in a string for the arabic equivalent?

  23. 23

    How to replace the last char of a string in bash with another char

  24. 24

    How to replace all occurrences of a string except the first one in JavaScript?

  25. 25

    How to replace a string between double brackets while typing?

  26. 26

    How to replace a single slash '/' in a string excluding the slash characters in the '://' pattern

  27. 27

    How to find and replace a string with PowerShell - issue with Eastern European characters

  28. 28

    How to strip or replace all matches of a pattern from a string in Bash?

  29. 29

    How to replace random parts of string with random keys from array?

ホットタグ

アーカイブ