如何使用Python 3 删除字符串重音符号?
字符串重音是从其他重音语言改编而来的特殊字符串字符。在本文中,我们将删除字符串中的 ascents 。
例子:
Input: orčpžsíáýd
Output: orcpzsiayd
Input: stävänger
Output: stavanger
我们可以使用名为Unidecode的Python模块从字符串中删除重音符号。该模块包含一个方法,该方法接受一个 Unicode 对象或字符串并返回一个没有上升的字符串。
句法:
output_string = unidecode.unidecode(target_string )
下面是一些描述如何从字符串中删除上升的示例:
示例 1:
Python3
# import required module
import unidecode
# assign string
string = "orčpžsíáýd"
# display original string
print('\nOriginal String:', string)
# remove ascents
outputString = unidecode.unidecode(string)
# display new string
print('\nNew String:', outputString)
Python3
# import required module
import unidecode
# assign string
string = "stävänger"
# display original string
print('\nOriginal String:', string)
# remove ascents
outputString = unidecode.unidecode(string)
# display new string
print('\nNew String:', outputString)
Python3
# import required module
import unidecode
# assign string
stringList = ["hell°", "tromsø", "stävänger", "ölut"]
# display original string
print('\nOriginal List of Strings:\n', stringList)
for i in range(len(stringList)):
# remove ascents
stringList[i] = unidecode.unidecode(stringList[i])
# display new string
print('\nNew List of Strings:\n', stringList)
输出:
Original String: orčpžsíáýd
New String: orcpzsiayd
示例 2:
蟒蛇3
# import required module
import unidecode
# assign string
string = "stävänger"
# display original string
print('\nOriginal String:', string)
# remove ascents
outputString = unidecode.unidecode(string)
# display new string
print('\nNew String:', outputString)
输出:
Original String: stävänger
New String: stavanger
示例 3:
蟒蛇3
# import required module
import unidecode
# assign string
stringList = ["hell°", "tromsø", "stävänger", "ölut"]
# display original string
print('\nOriginal List of Strings:\n', stringList)
for i in range(len(stringList)):
# remove ascents
stringList[i] = unidecode.unidecode(stringList[i])
# display new string
print('\nNew List of Strings:\n', stringList)
输出:
Original List of Strings:
['hell°', 'tromsø', 'stävänger', 'ölut']
New List of Strings:
['helldeg', 'tromso', 'stavanger', 'olut']