Обработка строки с символами другой кодировки

Petr

Member
Есть строка в которой есть символы ?�, пытаюсь заменить их на "n", но падает ошибка
Код:
import re

str = "Espa?�ola"
x = "?�"
y = "n"

if re.search(x,str) != None:
print ('is in str')
new_str = str.replace(x,y)
print(new_str)
else:
print ('NO '+ x +' in str[' + str + "]" )
результат выполнения
Код:
Traceback (most recent call last):
File "main.py", line 7, in <module>
if re.search(x,str) != None:
File "/usr/lib/python3.8/re.py", line 201, in search
return _compile(pattern, flags).search(string)
File "/usr/lib/python3.8/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib/python3.8/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/usr/lib/python3.8/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/usr/lib/python3.8/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/usr/lib/python3.8/sre_parse.py", line 668, in _parse
raise source.error("nothing to repeat",
re.error: nothing to repeat at position 0
 

Stepan Mikulov

Administrator
Команда форума
Код:
str_ = "Espa?�ola"
x = "?�"
y = "n"
new_str = ''

if x in str_:
new_str = str_.replace(x, y)
print(new_str)
else:
print('NO ' + x +' in str[' + str_ + "]")
 
Сверху