string='I’m celebrating my sixth month anniversary of no longer being a customer of Star Wars. I’ve saved a lot of money'
Run Code Online (Sandbox Code Playgroud)
从上面的字符串我想摆脱所有的特殊字符.
如果您只想删除所有非ASCII字符string:
print(''.join(c for c in string if ord(c) < 128))
Run Code Online (Sandbox Code Playgroud)
这输出:
Im celebrating my sixth month anniversary of no longer being a customer of Star Wars. Ive saved a lot of money
Run Code Online (Sandbox Code Playgroud)
但是,最好找出原始字符串的编码是什么,并使用bytes.decode它将其转换为原始内容而不删除任何内容.