我正在努力编码一个字符串,其中scandic字母是utf-8格式。
例如,我想转换以下内容
字符串:
test_string = "\xc3\xa4\xc3\xa4abc"
以以下形式:
test_string = "ääabc"
最终目标是通过API将此字符串发送到Slack通道。我做了一些测试,发现Slack可以正确处理scandic字母。
我尝试了以下命令:
test_string= test_string.encode('latin1').decode('utf-8')
但这根本不会改变字符串
同样的情况也适用于更暴力的方法:
def simple_scand_convert(string):
string = string.replace("\xc3\xa4", "ä")
同样,这根本不会改变字符串。我可以从哪里找到解决方案的技巧或材料
我无法重现您从传入的webhook代码片段中读取soup消息的;因此,我的答案基于硬编码数据,并详细说明了Python specific text encodings ^{} and ^{} 的工作原理:
输出:
\SO\68069394.py
根据最初的问题和评论中的讨论,我怀疑您只是没有保存转换的结果。Python字符串是不可变的,因此仅对传递到函数中的字符串进行更改不会对原始字符串产生任何影响:
相反,您需要在函数中返回转换结果并重新分配变量:
相关问题 更多 >
编程相关推荐