Python 如何在不使用任何外部库的情况下从字符串中删除特殊字符?

Python 如何在不使用任何外部库的情况下从字符串中删除特殊字符?,python,python-3.x,Python,Python 3.x,我知道有unicode库,但我不想导入任何东西(这是针对导入库会丢失点数的赋值) 假设我有这个字符串“áèùùìssaáò”,所需的输出将是“AEEUUISSAAO”。在Python中有这样做的方法吗?这段代码应该可以工作 string = "àèéùùìssaààò" string = string.replace("à", "a") string = string.replace("è", "e&quo

我知道有unicode库,但我不想导入任何东西(这是针对导入库会丢失点数的赋值)

假设我有这个字符串
“áèùùìssaáò”
,所需的输出将是
“AEEUUISSAAO”
。在Python中有这样做的方法吗?

这段代码应该可以工作

string = "àèéùùìssaààò"
string = string.replace("à", "a")
string = string.replace("è", "e")
string = string.replace("é", "e")
string = string.replace("ù", "u")
string = string.replace("ò", "o")
string = string.replace("ì", "i")
print(string)
您可以使用string.replace方法替换重音符号。
您可以像这样使用string.replace
string.replace(旧的,新的,[count])

简单一点

string = "àèéùùìssaààò"
replacelist = ["à", "è" ,"é", "ù", "ò", "ì"] # add the accent to here
correctlist = ["a", "e", "e", "u", "o", "i"] # add the normal English to here
for i in range(len(replacelist)):
    string = string.replace(replacelist[i], correctlist[i])
print(string)
这是一个for循环,所以更容易一点。
您只需要在replacelist和correctlist中添加一些内容。

这段代码应该可以工作

string = "àèéùùìssaààò"
string = string.replace("à", "a")
string = string.replace("è", "e")
string = string.replace("é", "e")
string = string.replace("ù", "u")
string = string.replace("ò", "o")
string = string.replace("ì", "i")
print(string)
您可以使用string.replace方法替换重音符号。
您可以像这样使用string.replace
string.replace(旧的,新的,[count])

简单一点

string = "àèéùùìssaààò"
replacelist = ["à", "è" ,"é", "ù", "ò", "ì"] # add the accent to here
correctlist = ["a", "e", "e", "u", "o", "i"] # add the normal English to here
for i in range(len(replacelist)):
    string = string.replace(replacelist[i], correctlist[i])
print(string)
这是一个for循环,所以更容易一点。

您只需要在replacelist和correctlist中添加一些内容。

一种快速方法,可以扫描字符串一次

string = "àèéùùìssaààò"
lookup = {"à": "a", "è": "e", "é": "e", "ù": "u", "ò": "o", "ì": "i"}
clean_string = ''.join(lookup.get(x, x) for x in string)
print(clean_string)
输出

aeeuuissaaao

一种可以扫描字符串一次的快速方法

string = "àèéùùìssaààò"
lookup = {"à": "a", "è": "e", "é": "e", "ù": "u", "ò": "o", "ì": "i"}
clean_string = ''.join(lookup.get(x, x) for x in string)
print(clean_string)
输出

aeeuuissaaao

可能相关:这使我需要导入unicode,但我不想导入任何东西。您不想导入Python标准模块的确切原因是什么?这是为了一个assignment,我每次导入东西都会损失5分。分数是10分。6/10是Passpobly-related的最低要求:这使我导入unicode,但我不想导入任何东西。您不想导入Python标准模块的确切原因是什么?这是为了一个assignment,我每次导入东西都会损失5分。分数是10分。6/10是Pass的最低要求。如果不对我必须更改的字母进行硬编码,就无法做到这一点吗?@KrankenWagen编辑,因此有更简单的方法。但我认为,如果不导入某些内容,不对您必须更改的字母进行硬编码,就无法做到这一点。如果不对我必须更改的字母进行硬编码,就无法做到这一点?@KrankenWagen编辑,因此有更简单的方法。但是我认为,如果不导入一些东西,不硬编码您必须更改的字母,就无法做到这一点。