简体   繁体   English

如何从python中的字符串中删除这些特殊的ascii字符?

[英]How to remove these special ascii characters from string in python?

string='I’m celebrating my sixth month anniversary of no longer being a customer of Star Wars. I’ve saved a lot of money'

从上面的字符串中,我想摆脱所有特殊字符。

If you simply want to remove all non-ASCII characters from string : 如果您只想从string删除所有非ASCII字符:

print(''.join(c for c in string if ord(c) < 128))

This outputs: 输出:

Im celebrating my sixth month anniversary of no longer being a customer of Star Wars. Ive saved a lot of money

However, it's better that you find out what the encoding of your original string is and use bytes.decode to convert it to its original content with nothing removed. 但是,最好找出原始字符串的编码,然后使用bytes.decode将其转换为原始内容, bytes.decode删除任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM