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