我有字符串“ Leichtbewölkt”。 “ö”导致错误
'ascii' codec can't encode character u'\xf6' in position 10: ordinal not in range(128)
我尝试将其编码为utf8#-*-编码:如果文件不起作用,则将utf-8-*-从头开始。你能帮我吗?我只想将其打印到命令行中并将其发送到arduino。
def removeThat(schasch):
print(schasch)
schasch = str(schasch).encode('utf8')
schasch = str(schasch).encode('utf8').replace("ü","ue").replace("ä","ae").replace("ö","oe").replace("ß","sss")
return schasch
答案 0 :(得分:0)
在将字符串编码为utf8之前先替换字符
writerow()
可打印
replacements = {
'ü': 'ue',
'ä': 'ae',
'ö': 'oe',
'ß': 'ss',
}
def replace_umlauts(text: str) -> str:
for find, replace in replacements.items():
text = text.replace(find, replace)
return text
def encode_text(text: str) -> bytes:
fixed = replace_umlauts(text)
return fixed.encode('utf-8')
if __name__ == '__main__':
text = 'Leicht bewölkt'
print(replace_umlauts(text))
print(encode_text(text))